2

如何以material slider编程方式更改气泡字体?

在此处输入图像描述

4

4 回答 4

2

您必须覆盖滑块的材质主题。

    <style name="Myslider" parent="@style/Widget.MaterialComponents.Slider">
        <item name="labelStyle">@style/My_Tooltip</item>
        <item name="materialThemeOverlay">@style/ThemeOverlay.Slider</item>
        <item name="android:fontFamily">@font/iransans</item>
    </style>

    <style name="My_Tooltip" parent="Widget.MaterialComponents.Tooltip">
        <!-- font for your slider  tooltip -->
        <item name="android:fontFamily">//font</item>
    </style>

    <style name="ThemeOverlay.Slider" parent="">
        <item name="android:fontFamily">@font/iransans</item>
    </style>
于 2021-03-12T17:24:17.327 回答
2

目前 ( 1.3.0) 定义 the和intextAppearance使用的字体的唯一方法是使用样式:TooltipSlider

<style name="App.Slider" parent="@style/Widget.MaterialComponents.Slider">
    <item name="labelStyle">@style/App.Tooltip</item>
</style>

<style name="App.Tooltip" parent="Widget.MaterialComponents.Tooltip">
    <item name="android:textAppearance">@style/TextAppearance.MaterialComponents.Tooltip</item>
</style>

如果您想使用反射(我不鼓励),您必须访问类中List<TooltipDrawable> labels定义的BaseSlider
然后你可以使用 类中定义的方法setTextAppearance或方法setTextAppearanceResourceTooltipDrawable

于 2021-03-16T16:29:36.360 回答
1

根据Gabriele Mariotti 的回答,您可以使用反射以编程方式更改滑块字体。您必须访问 BaseSlider 上的私有字段“标签”才能访问“TooltipDrawable”列表,您可以在其中使用自定义 Tooltip TextAppearance 更改每个 TooltipDrawable。

我为此实现了一个辅助函数:

    @SuppressLint("RestrictedApi")
    public static void changeSliderTypeFace(Slider slider, @StyleRes int textAppearanceStyleResId){

        try
        {
            Class baseSliderCls = Slider.class.getSuperclass();
            if (baseSliderCls != null) {
                Field privateLabelsField = baseSliderCls.getDeclaredField("labels");
                privateLabelsField.setAccessible(true);
                List<TooltipDrawable> tooltipDrawableList = (List<TooltipDrawable>) privateLabelsField.get(slider);
                if(tooltipDrawableList!=null && tooltipDrawableList.size()>0)
                {
                    for(TooltipDrawable tooltipDrawable : tooltipDrawableList){
                        tooltipDrawable.setTextAppearance(new TextAppearance(slider.getContext(), textAppearanceStyleResId));
                    }
                }
            }
        }
        catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

你可以像下面这样使用它:

Slider slider = findViewById(R.id.slider);
changeSliderTypeFace(slider, R.style.CustomTooltipTextAppearance);

其中R.style.CustomTooltipTextAppearance是您在 styles.xml 中定义的自定义工具提示 TextAppearance 样式,如下所示:

<style name="CustomTooltipTextAppearance" parent="TextAppearance.MaterialComponents.Tooltip">
        <item name="android:textColor">@android:color/white</item>
        <item name="fontFamily">@font/roboto_mono</item>
        <item name="android:fontFamily">@font/roboto_mono</item>
</style>

结果: 滑块新机器人单声道字体

于 2021-03-17T12:07:59.193 回答
1

我找到了这个链接;它描述的是在 xml 中创建字体并在布局和小部件中以编程方式使用它。

xml中的字体

愚蠢的滑块不会暴露字体属性。向测试项目添加了一个滑块,它是第一个答案与允许以编程方式将自定义滑块添加到视图的代码的组合。

欢迎任何建议。现在,即使我以编程方式将滑块添加到视图中,字体也只会在样式应用于所有滑块时才会改变。真可惜。我最好的猜测是父视图覆盖了自定义字体属性。

该方法似乎适用于其他小部件。我认为在飞行中这样做会很酷。这类东西的许多应用程序。

以编程方式样式

样本有一个几乎可以工作的滑块

于 2021-03-13T01:06:28.893 回答