0

我的文字与拇指不对齐。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

我的标签要离开屏幕了。这是我的科特林代码:

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"/>
4

1 回答 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);
            //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) {

        }
    });

这里是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" />
于 2020-01-06T10:37:53.390 回答