带有工具提示的Seekbar android
大家好 我正试图用工具提示定制android seekbar,就像给定的图片一样。 有没有办法在seekbar中用拇指添加textview。或任何其他想法带有工具提示的Seekbar android,android,seekbar,Android,Seekbar,大家好 我正试图用工具提示定制android seekbar,就像给定的图片一样。 有没有办法在seekbar中用拇指添加textview。或任何其他想法 谢谢已经有人问过了。他们使用文本视图,您可以使用图像视图或自定义视图来满足您的需要 看看这个例子,你可能会从中得到一些想法 我们可以通过获得拇指的边界来实现。并在seekbar的progressChanged中设置textview的x @Override public void onProgressChanged(SeekBar seekBa
谢谢已经有人问过了。他们使用文本视图,您可以使用图像视图或自定义视图来满足您的需要 看看这个例子,你可能会从中得到一些想法
我们可以通过获得拇指的边界来实现。并在seekbar的progressChanged中设置textview的x
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mDistance.setText("" + AppConstants.getDistance() + " miles");
//Get the thumb bound and get its left value
int x = mSeekBar.getThumb().getBounds().left;
//set the left value to textview x value
mDistance.setX(x);
}
Bora(很棒)答案的一些优化: 并宣布:
private RelativeLayout.LayoutParams seekBarLayoutParams;
seekBarLayoutParams = (RelativeLayout.LayoutParams) seekbar.getLayoutParams();
//or `LinearLayout` if you're using `LinearLayout`.
private float seekBarX;
int[] location = new int[2];
seekBarX = location[0];
这正好把它放在拇指中间,即使你的代码>搜索栏< /代码>有一个边距。
不过,它不处理
Y
位置。为了解决这个问题,如果您的SeekBar
有一个marginTop
,请将其删除,只需将marginBottom
添加到它上面的元素(xml格式)。下面的计算对我有效。非常简单和合乎逻辑
ViewGroup.MarginLayoutParams seekBarParams = (ViewGroup.MarginLayoutParams) seekBar.getLayoutParams();
// Calculate x position
float x = seekBarParams.leftMargin + seekBar.getPaddingLeft() + seekBar.getThumb().getBounds().left - tvValue.getWidth()/2;
tvValue.setX(x);
// Calculate y position
float y = seekBarParams.topMargin + seekBar.getPaddingTop() - tvValue.getHeight();
tvValue.setY(y);
实现此方法所需的API级别为16或以上!
ViewGroup.MarginLayoutParams seekBarParams = (ViewGroup.MarginLayoutParams) seekBar.getLayoutParams();
// Calculate x position
float x = seekBarParams.leftMargin + seekBar.getPaddingLeft() + seekBar.getThumb().getBounds().left - tvValue.getWidth()/2;
tvValue.setX(x);
// Calculate y position
float y = seekBarParams.topMargin + seekBar.getPaddingTop() - tvValue.getHeight();
tvValue.setY(y);