Seekbar-在拇指上方对齐值标签

问题描述 投票:0回答:1

我的文字与拇指不对齐。

enter image description here

enter image description here

enter image description here

我的标签在屏幕上消失了。这是我的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"/>
android seekbar android-seekbar seekbar-thumb
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" />
© www.soinside.com 2019 - 2024. All rights reserved.