如何动态更改textview位置?

时间:2016-02-04 11:21:58

标签: java android xml seekbar

我有一个textview,显示导引条的值。我希望在擦除器/滑块顶部显示该文本视图,因为它是由用户左右拉动的。 我需要做什么代码?

    SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
    final TextView seekBarValue = (TextView)findViewById(R.id.seekervalue);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            // TODO Auto-generated method stub
            seekBarValue.setText(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });

1 个答案:

答案 0 :(得分:-1)

只需在TextView.setX(float X)方法中使用TextView.setY(float Y)(如果您需要垂直移动)onProgressChanged

根据您的需要调整值。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv=(TextView)findViewById(R.id.textView);
        SeekBar sb=(SeekBar)findViewById(R.id.seekBar);
        final float startpos=tv.getX();
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv.setX(startpos+progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}