温馨提示×

android中preference怎么设置字号和颜色

小亿
111
2024-03-01 17:09:27
栏目: 编程语言

在Android中,Preference的字号和颜色可以通过创建自定义的Preference布局来实现。首先,在res/layout文件夹下创建一个XML布局文件,例如custom_preference.xml,然后在这个文件中定义Preference的样式,可以设置文本大小和颜色等属性。例如:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
        android:key="custom_preference"
        android:title="Custom Preference"
        android:layout="@layout/custom_preference_layout"/>

</PreferenceScreen>

然后在res/layout文件夹下创建一个custom_preference_layout.xml文件,定义Preference的样式,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/colorPrimary"/>

</LinearLayout>

在这个布局文件中,设置了TextView的文本大小和颜色。最后,在PreferenceActivity中加载这个自定义的Preference布局文件,例如:

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.custom_preference);
    }
}

这样就可以在Preference中设置字号和颜色了。

0