3

我正在尝试让 TextView 跟随进度条上的拇指并在 TextView 中显示进度的(肯定很简单?)任务。

问题是,对于小于最大一半的进度值,TextView 漂移到拇指的左侧,越来越多地离开正确的位置,反之亦然,进度值超过最大值的一半,TextView 越来越多地漂移到正确的。

以下是重现问题的代码版本...

布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical" 
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Seekbar_test" >

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="59"
    android:layout_marginTop="24dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge" />

和.java

package com.example.seekbar_test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class Seekbar_test extends Activity {
SeekBar fade_seek;
TextView fade_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seekbar_test);

    fade_seek = (SeekBar) findViewById(R.id.seekBar1);
    fade_text = (TextView) findViewById(R.id.textView1);

    fade_seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       
        @Override       
        public void onStopTrackingTouch(SeekBar seekBar) {            
        }       
        @Override       
        public void onStartTrackingTouch(SeekBar seekBar) {     
        }       
        @Override       
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
            say_minutes_left(progress);
        }
    });
}

private void say_minutes_left(int how_many)
{
    String what_to_say = String.valueOf(how_many);
    fade_text.setText(what_to_say);

    int seek_label_pos = (int)((float)(fade_seek.getMeasuredWidth()) * ((float)how_many / 60f));
    fade_text.setX(seek_label_pos);
}
}
4

6 回答 6

1

这对我有用:

private void say_minutes_left(int how_many)
{
    String what_to_say = String.valueOf(how_many);
    fade_text.setText(what_to_say);
    int seek_label_pos = (((fade_seek.getRight() - fade_seek.getLeft()) * fade_seek.getProgress()) / fade_seek.getMax()) + fade_seek.getLeft();
    if (how_many <=9)
        {
            fade_text.setX(seek_label_pos - 6);
        }
    else
        {
            fade_text.setX(seek_label_pos - 11);
        }
}

感谢 Pushpendra Kuntal 和 flightplanner @无法在 seekbar 的拇指中获得 texBox 的正确位置

于 2013-04-13T18:39:32.663 回答
1

以下解决方案适用于 API >= 15:

  1. 覆盖 SeekBar 以创建在 api 15 上不可用的 getThumb() 方法:

    public class CustomSeekBar extends SeekBar {
        private Drawable thumb;
        public CustomSeekBar(Context context) {
            super(context);
        }
    
        public CustomSeekBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void setThumb(Drawable thumb) {
            super.setThumb(thumb);
            this.thumb = thumb;
        }
    
        @Override
        public Drawable getThumb() {
            return thumb;
        }
    }
    
  2. 并像这样使用它:

    correctX = customSeekBar.getThumb().getBounds().left;
    
于 2017-02-04T23:21:52.177 回答
0

适用于 Ken Nichols 代码,但这是一个更好的决定,例如,将 TextView 居中到 ProgressBar(SeekBar) 中的当前位置:

 private void say_minutes_left(int how_many)
{
    String what_to_say = String.valueOf(how_many);
    fade_text.setText(what_to_say);
    int seek_label_pos = (((fade_seek.getRight() - fade_seek.getLeft()) * fade_seek.getProgress()) / fade_seek.getMax()) + fade_seek.getLeft();
  fade_text.setX(seek_label_pos - fade_text.getWidth() / 2);
}

如果您的 ProgressBar(SeekBar) 中有填充,则可以使用此代码的更通用版本:

 private void say_minutes_left(int how_many)
    {
        String what_to_say = String.valueOf(how_many);
        fade_text.setText(what_to_say);
        int left = fade_seek.getLeft() + fade_seek.getPaddingLeft();
        int right = fade_seek.getRight() - fade_seek.getPaddingRight();
        int seek_label_pos = (((right - left * fade_seek.getProgress()) / fade_seek.getMax()) + left;
      fade_text.setX(seek_label_pos - fade_text.getWidth() / 2);
    }
于 2015-04-20T12:28:30.757 回答
0

使用它,我们正在根据进度计算 x 位置并将其设置为 textview

seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {

               var xPosition=  (((seekBar1.right - seekBar1.left) / seekBar1.max) * seekBar1.progress ) + seekBar1.left
               textView1.translationX = xPosition.toFloat() - (textView1.width/2)
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {
        }

        override fun onStopTrackingTouch(seekBar: SeekBar?) {
        }

    })
于 2020-04-10T19:32:53.070 回答
0
   //Get the thumb bound and get its left value
    int x = seekBar.getThumb().getBounds().left;
    //set the left value to textview x value
    textView.setX(x);

使用它根据进度移动文本视图

于 2016-10-25T06:33:48.450 回答
-1

文本视图必须是

 <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        />

并删除它:

  int seek_label_pos = (int)((float)(fade_seek.getMeasuredWidth()) * ((float)how_many / 60f));
  fade_text.setX(seek_label_pos);

希望有帮助。

于 2013-04-11T11:18:44.210 回答