英文:
Seekbar - align value label above thumb
问题
我的文本与滑块不对齐。
我的标签超出了屏幕。这是我的 Kotlin 代码:
val x: Int = progress * (seekBar!!.width - 2 * seekBar.thumbOffset) / seekBar.max
text12.text = "$progress %"
val textViewX: Int = x - text12.width / 2
val finalX =
if (text12.width + textViewX > maxX) {
maxX - text12.width - 100
} else textViewX + 40 /*your margin*/
text12.x = if (finalX < 0) 0f /*your margin*/ else finalX.toFloat() + 16
我的 XML:
<SeekBar
android:id="@+id/customSeekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:layout_marginTop="100dp"
android:progress="50"
app:layout_constraintTop_toBottomOf="@+id/tv_user_data_notsaved" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:text="12%"
app:layout_constraintBottom_toTopOf="@+id/customSeekBar1"
app:layout_constraintLeft_toLeftOf="parent" />
英文:
My text isn´t aligned with the thumb.
My label is going of the screen. This is my kotlin code:
val x: Int = progress * (seekBar!!.width - 2 * seekBar.thumbOffset) / seekBar.max
text12.text = "$progress %"
val textViewX: Int = x- text12.width / 2
val finalX =
if (text12.width + textViewX > maxX){
maxX - text12.width- 100
} else textViewX +40 /*your margin*/
text12.x = if (finalX < 0) 0f /*your margin*/ else finalX.toFloat() +16
My xml:
<SeekBar
android:id="@+id/customSeekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:layout_marginTop="100dp"
android:progress="50"
app:layout_constraintTop_toBottomOf="@+id/tv_user_data_notsaved"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:text="12%"
app:layout_constraintBottom_toTopOf="@+id/customSeekBar1"
app:layout_constraintLeft_toLeftOf="parent"/>
答案1
得分: 1
这是我的代码,在我的项目中正常工作:
seekBarWithHint.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("" + progress + " KM");
SmartApplication.REF_SMART_APPLICATION.writeSharedPreferences(Constants.KM, progress);
// 获取拇指的边界并获取其左侧值
x = seekBar.getThumb().getBounds().left;
// 将左侧值设置为文本视图的 x 值
textView.setX(x);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
以下是XML代码:
<SeekBar
android:id="@+id/seekBar_luminosite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/_22sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginRight="@dimen/_22sdp"
android:maxWidth="15dp"
android:maxHeight="12dp"
android:minWidth="15dp"
android:minHeight="12dp"
android:progressDrawable="@drawable/custom_seekbar"
android:splitTrack="false"
android:thumb="@drawable/custom_thumb" />
英文:
here is my code is working fine in my project
seekBarWithHint.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("" + progress + " KM");
SmartApplication.REF_SMART_APPLICATION.writeSharedPreferences(Constants.KM, progress);
//Get the thumb bound and get its left value
x = seekBar.getThumb().getBounds().left;
//set the left value to textview x value
textView.setX(x);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
here xml code
<SeekBar
android:id="@+id/seekBar_luminosite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/_22sdp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginRight="@dimen/_22sdp"
android:maxWidth="15dp"
android:maxHeight="12dp"
android:minWidth="15dp"
android:minHeight="12dp"
android:progressDrawable="@drawable/custom_seekbar"
android:splitTrack="false"
android:thumb="@drawable/custom_thumb" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论