简体   繁体   English

如何更改 SwitchPreference 的 fontFamily 和 textSize?

[英]How can i change fontFamily and textSize of SwitchPreference?

How can i change those font attributes of a SwitchPreference?如何更改 SwitchPreference 的这些字体属性?

I already tried to use app:layout attribute with the following layout:我已经尝试使用具有以下布局的app:layout属性:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@android:id/title"
        style="@android:style/TextAppearance.Widget.TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="MY-FONT"
        android:text="Title" />

    <TextView
        android:id="@android:id/summary"
        style="@android:style/TextAppearance.Widget.TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="MY-FONT"
        android:text="Summary" />    
</LinearLayout>

That doesn't work well, because the switch was missing and everything looks a bit messy.那效果不佳,因为缺少开关,而且一切看起来都有些凌乱。

What is an easy (and working) way to customize the fontfamily and textSize?自定义 fontfamily 和 textSize 的简单(和有效)方法是什么?

You need to create a Custom Switch Preference by extending its main class您需要通过扩展其主类来创建自定义开关首选项

public class CustomSwitchPreference extends SwitchPreference {
    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomSwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSwitchPreference(Context context) {
        super(context);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        TextView title1= (TextView) holder.findViewById(android.R.id.title);
        title1.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
        TextView title2= (TextView) holder.findViewById(android.R.id.summary);
        title2.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
    }
}

and use this is in your preferences xml并在您的首选项 xml 中使用它

from

<SwitchPreference 
   .....
/>

to

<com.example.myapp.CustomSwitchPreference
....
/>

This Worked for me这对我有用

To change the font family in android first you need to have the font in your app something like this Your font goes in the font directory under res要首先更改 android 中的字体系列,您需要在您的应用程序中使用类似这样的字体您的字体位于 res 下的字体目录中

Now to use it, your going in the right way现在使用它,你的方式是正确的

 <Button
            ...

            android:fontFamily="@font/nameOfFont"
            ... />

Let me now if it works for you如果它对你有用,现在让我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM