搜索栏 - 显示上面的TextView进度 [英] SeekBar - Showing progress with TextView above

查看:188
本文介绍了搜索栏 - 显示上面的TextView进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个TextView遵循一个进度条上的拇指和显示TextView的进步的(当然简单?)的任务。

的问题是,对于超过一半最大值以下的进展值,则TextView的漂移到拇指的左侧,得到正确的位置,并且反之亦然进展越来越左边值大于一半最大值多个TextView的漂移越来越更多的权利。

下面是code的一个版本能重现问题...

Layout.xml

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:方向=垂直
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=>中Seekbar_test。<搜索栏
    机器人:ID =@ + ID / seekBar1
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:最大=59
    机器人:layout_marginTop =24dp/><的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?

和.java

 包com.example.seekbar_test;
进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.SeekBar;
进口android.widget.SeekBar.OnSeekBarChangeListener;
进口android.widget.TextView;公共类Seekbar_test延伸活动{
搜索栏fade_seek;
TextView的fade_text;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_seekbar_test);    fade_seek =(搜索栏)findViewById(R.id.seekBar1);
    fade_text =(的TextView)findViewById(R.id.textView1);    fade_seek.setOnSeekBarChangeListener(新OnSeekBarChangeListener(){
        @覆盖
        公共无效onStopTrackingTouch(搜索栏搜索栏){
        }
        @覆盖
        公共无效onStartTrackingTouch(搜索栏搜索栏){
        }
        @覆盖
        公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER){
            say_minutes_left(进度);
        }
    });
}私人无效say_minutes_left(INT HOW_MANY)
{
    字符串what_to_say =将String.valueOf(HOW_MANY);
    fade_text.setText(what_to_say);    INT seek_label_pos =(INT)((浮点)(fade_seek.getMeasuredWidth())*((浮点)HOW_MANY / 60F));
    fade_text.setX(seek_label_pos);
}
}


解决方案

工作好与肯·尼科尔斯code,但这里是一个更好的决策,例如,中心,你的TextView在进度(搜索栏)当前位置:

 私人无效say_minutes_left(INT HOW_MANY)
{
    字符串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);
}

I'm trying the (surely simple?) task of having a TextView follow the thumb on a progress bar and show the progress in the TextView.

The problem is that for progress values of less than half max, the TextView drifts to the left of the thumb, getting more and more left of the correct place and vice versa with progress values more than half max the TextView drifts more and more to the right.

Below is a version of code that reproduces the problem...

Layout.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" />

And .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);
}
}

解决方案

Works good with Ken Nichols code, but here is a better decision, for example, to center you TextView to current position in 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);
}

这篇关于搜索栏 - 显示上面的TextView进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆