在 Android 中创建 SeekBar 位置弹出窗口

发布于 2024-11-18 06:38:05 字数 128 浏览 4 评论 0原文

我的应用程序中有一个 SeekBar,我希望它的行为使得当用户拖动“拇指”时,拇指上方会出现一个弹出窗口(并在拖动拇指时跟随它),并且弹出窗口显示相应的位置SeekBar 的(或该位置可能代表的任何其他文本)。

有什么建议吗?

I have a SeekBar in my application, and I want it to behave so that when the user is dragging the 'thumb', a Popup appears above the thumb (and follows it as the thumb is dragged), and the Popup displays the corresponding positon of the SeekBar (or any other text that position may represent).

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

素手挽清风 2024-11-25 06:38:05

查看此博客

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
   // TODO Auto-generated method stub
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.ABOVE, seekBar.getId());
        Rect thumbRect = bar.getSeekBarThumb().getBounds();
        p.setMargins(
        thumbRect.centerX(),0, 0, 0);
        textView.setLayoutParams(p);
        textView.setText(String.valueOf(progress));
    }
  });
 }
}

Check out this blog

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
   // TODO Auto-generated method stub
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.ABOVE, seekBar.getId());
        Rect thumbRect = bar.getSeekBarThumb().getBounds();
        p.setMargins(
        thumbRect.centerX(),0, 0, 0);
        textView.setLayoutParams(p);
        textView.setText(String.valueOf(progress));
    }
  });
 }
}
旧话新听 2024-11-25 06:38:05

拇指是可绘制的,因此解决方案可能是将拇指设置为可绘制的,其中包含拇指和带有文本的弹出窗口。这意味着您需要为滑块上的每个可能值提供一个可绘制/图标,并在每次值更改时更新它。环顾四周,我发现了这一点,这表明其他人在可绘制对象中需要文本时已经采用了这种方式: Convert String到可绘制

The thumb is a drawable, so a solution could be to set the thumb to a drawable that includes both thumb and popup with text. That means you would need a drawable/icon for each possible value on your slider, and update it every time the value changes. Looking around I found this, which suggests that others have gone that way when they needed text in their drawables: Convert String to Drawable

一个人的旅程 2024-11-25 06:38:05

我为搜索栏后面的弹出窗口找到的解决方案是将文本视图放在搜索栏正上方的相对布局中。然后,在seekbar的onProgressChange中,通过一些调整来相应地设置textView的左边距

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(widht, height)
params.setLeftMargin(margin)

,效果很好!

干杯

The solution I found for this popup following the seek bar is to put the textview in a relative layout right above the seekbar. Then, in seekbar's onProgressChange, set the textView's left margin accordingly using

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(widht, height)
params.setLeftMargin(margin)

works great, with some tweaking!

cheers

牵强ㄟ 2024-11-25 06:38:05

这是所选答案的 Kotlin 版本和更多细节

override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
    txtSeekMonthsPopup.text = progress.toString()
    val params: RelativeLayout.LayoutParams = 
    txtSeekMonthsPopup.layoutParams as RelativeLayout.LayoutParams
    val existingMargin = params.marginStart
    params.marginStart = (seekBar?.thumb?.bounds?.left?: existingMargin) + 20 // 20 makes the text view more centered on the thumb
}

Here's the Kotlin version of the chosen answer and a bit more detail

override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
    txtSeekMonthsPopup.text = progress.toString()
    val params: RelativeLayout.LayoutParams = 
    txtSeekMonthsPopup.layoutParams as RelativeLayout.LayoutParams
    val existingMargin = params.marginStart
    params.marginStart = (seekBar?.thumb?.bounds?.left?: existingMargin) + 20 // 20 makes the text view more centered on the thumb
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文