android - SeekBar - 使用上面的 TextView 显示进度

标签 android scrollbar seekbar

我正在尝试(肯定很简单?)让 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);
}
}

最佳答案

这对我有用:

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 和飞行计划员 @ unable to get right position of texBox in Thumb of seekbar

关于android - SeekBar - 使用上面的 TextView 显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15946858/

相关文章:

android - SeekBarPreference/NumberPickerPreference ClassCastException 异常

android - NavigationDrawer 中的 SeekBar

android - 您如何完全使用 Robotium 对 Android 应用程序进行功能测试

android - MapView.onMapReady 从未在 Fragment 中调用以在 MapView 中加载 Google map

javascript - 如何只为 div 标签内的表格维护滚动

excel - 当文本长度溢出用户表单文本框时触发事件

android - 我如何设计带有点的 android 搜索栏

android - 使用 Volley 进行响应处理

android - 使用后退按钮关闭 PopupWindow?

jQueryUI 对话框滚动位置重置为另一个对话框的焦点