如何使用 Material 组件的 Slider 显示标签?
How to display label using Slider of Material component?
我正在使用 Material 组件的滑块:
我试图在拇指移动时显示标签,这似乎受 labelFormatter 属性的支持。
我的代码如下所示:
Slider s = new Slider(context);
s.setLabelFormatter(new Slider.LabelFormatter() {
@NonNull
@Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
当我逐行使用调试器时,它会执行此函数:
private void drawLabelText(@NonNull Canvas canvas, int width, int top) {
labelTextPaint.getTextBounds(labelText, 0, labelText.length(), labelTextBounds);
int left = trackSidePadding + (int) (thumbPosition * width) - labelTextBounds.width() / 2;
canvas.drawText(labelText, left, top - labelTextTopOffset - thumbRadius, labelTextPaint); }
但是没有文本只显示滑块...
我是 android 的新手,我肯定错过了一些东西。
感谢您的帮助:)
编辑 1:
这里是整个代码的简化:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoordinatorLayout layout = new CoordinatorLayout(this);
setContentView(layout);
// I am Using Coordinator Layout for current activity so...
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Setting gravity to CENTER
layoutParams.gravity = Gravity.CENTER;
Slider slider = new Slider(this);
slider.setLabelFormatter(new Slider.LabelFormatter() {
@NonNull
@Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
layout.addView(slider, layoutParams);
}
但还是不行...
输出结果
编辑 2:
将 Material 组件从版本“1.2.0-alpha02”更新到版本“1.2.0-alpha05”解决了这个问题。
其实我想发表评论,但由于声誉限制,我不能,
看起来您正在以编程方式在 activity 中添加视图。我在这里实现了新的 MaterialComponent Slider 和 LabelFormatter
ViewGroup group = findViewById("YOUR_ACTIVITY_LAYOUT");
// I am Using Coordinator Layout for current activity so...
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Setting gravity to CENTER
layoutParams.gravity = Gravity.CENTER;
Slider slider = new Slider(this);
slider.setLabelFormatter(new Slider.LabelFormatter() {
@NonNull
@Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
group.addView(slider, layoutParams);
}
然后你去...
我正在使用 Material 组件的滑块:
我试图在拇指移动时显示标签,这似乎受 labelFormatter 属性的支持。
我的代码如下所示:
Slider s = new Slider(context);
s.setLabelFormatter(new Slider.LabelFormatter() {
@NonNull
@Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
当我逐行使用调试器时,它会执行此函数:
private void drawLabelText(@NonNull Canvas canvas, int width, int top) {
labelTextPaint.getTextBounds(labelText, 0, labelText.length(), labelTextBounds);
int left = trackSidePadding + (int) (thumbPosition * width) - labelTextBounds.width() / 2;
canvas.drawText(labelText, left, top - labelTextTopOffset - thumbRadius, labelTextPaint); }
但是没有文本只显示滑块...
我是 android 的新手,我肯定错过了一些东西。
感谢您的帮助:)
编辑 1:
这里是整个代码的简化:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoordinatorLayout layout = new CoordinatorLayout(this);
setContentView(layout);
// I am Using Coordinator Layout for current activity so...
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Setting gravity to CENTER
layoutParams.gravity = Gravity.CENTER;
Slider slider = new Slider(this);
slider.setLabelFormatter(new Slider.LabelFormatter() {
@NonNull
@Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
layout.addView(slider, layoutParams);
}
但还是不行...
输出结果
编辑 2:
将 Material 组件从版本“1.2.0-alpha02”更新到版本“1.2.0-alpha05”解决了这个问题。
其实我想发表评论,但由于声誉限制,我不能, 看起来您正在以编程方式在 activity 中添加视图。我在这里实现了新的 MaterialComponent Slider 和 LabelFormatter
ViewGroup group = findViewById("YOUR_ACTIVITY_LAYOUT");
// I am Using Coordinator Layout for current activity so...
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Setting gravity to CENTER
layoutParams.gravity = Gravity.CENTER;
Slider slider = new Slider(this);
slider.setLabelFormatter(new Slider.LabelFormatter() {
@NonNull
@Override
public String getFormattedValue(float value) {
return "MY STRING";
}
});
group.addView(slider, layoutParams);
}
然后你去...