Seekbar – align value label above thumb

huangapple go评论158阅读模式
英文:

Seekbar - align value label above thumb

问题

我的文本与滑块不对齐。

Seekbar – align value label above thumb

Seekbar – align value label above thumb

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.

Seekbar – align value label above thumb

Seekbar – align value label above thumb

Seekbar – align value label above 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 = &quot;$progress %&quot;
val textViewX: Int = x- text12.width / 2
val finalX =
    if (text12.width + textViewX &gt; maxX){
        maxX - text12.width- 100
    } else textViewX +40 /*your margin*/
text12.x = if (finalX &lt; 0) 0f /*your margin*/ else finalX.toFloat() +16

My xml:

&lt;SeekBar
    android:id=&quot;@+id/customSeekBar1&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:max=&quot;100&quot;
    android:layout_marginTop=&quot;100dp&quot;
    android:progress=&quot;50&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/tv_user_data_notsaved&quot;/&gt;

&lt;TextView
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:id=&quot;@+id/text1&quot;
    android:text=&quot;12%&quot;
    app:layout_constraintBottom_toTopOf=&quot;@+id/customSeekBar1&quot;
    app:layout_constraintLeft_toLeftOf=&quot;parent&quot;/&gt;

答案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(&quot;&quot; + progress + &quot; KM&quot;);
            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

 &lt;SeekBar
                    android:id=&quot;@+id/seekBar_luminosite&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:layout_gravity=&quot;center&quot;
                    android:layout_marginLeft=&quot;@dimen/_22sdp&quot;
                    android:layout_marginTop=&quot;@dimen/_10sdp&quot;
                    android:layout_marginRight=&quot;@dimen/_22sdp&quot;
                    android:maxWidth=&quot;15dp&quot;
                    android:maxHeight=&quot;12dp&quot;
                    android:minWidth=&quot;15dp&quot;
                    android:minHeight=&quot;12dp&quot;
                    android:progressDrawable=&quot;@drawable/custom_seekbar&quot;
                    android:splitTrack=&quot;false&quot;
                    android:thumb=&quot;@drawable/custom_thumb&quot; /&gt;

huangapple
  • 本文由 发表于 2020年1月6日 18:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定