如何更改SwitchPreference的fontFamily和textSize?

时间:2019-03-14 20:17:42

标签: android android-preferences preferencescreen

如何更改SwitchPreference的字体属性?

我已经尝试将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>

那行不通,因为缺少开关,而且一切看起来都有些混乱。

自定义fontfamily和textSize的简便(有效)方法是什么?

2 个答案:

答案 0 :(得分:0)

要首先在android中更改字体系列,您需要在应用中添加如下字体 Your font goes in the font directory under res

现在要使用它,您的操作方式正确

 <Button
            ...

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

现在就让我来看看吧

答案 1 :(得分:0)

您需要通过扩展其主类来创建“自定义开关首选项”

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));
    }
}

并在您的首选项xml中使用它

来自

<SwitchPreference 
   .....
/>

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

这对我有用

相关问题