你需要为此创造CustomPreference
和CustomPreferenceCategory
。包括在你CustomPreference
的CustomPreferenceCategory
preference.xml
自定义偏好:
public class CustomPreference extends Preference {
Typeface fontStyle;
public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
fontStyle = Typeface.createFromAsset(CA.getApplication().getApplicationContext().getAssets(), AppConstants.fontStyle);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTypeface(fontStyle);
titleView.setTextColor(Color.RED);
TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
summaryView.setTypeface(fontStyle);
summaryView.setTextColor(Color.RED);
}
}
自定义偏好类别:
public class CustomPreferenceCategory extends PreferenceCategory {
Typeface fontStyle;
public CustomPreferenceCategory(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreferenceCategory(Context context) {
super(context);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
fontStyle = Typeface.createFromAsset(CA.getApplication()
.getApplicationContext().getAssets(), AppConstants.fontStyle);
TextView titleView = (TextView) view.findViewById(android.R.id.title);
titleView.setTypeface(fontStyle);
// titleView.setTextColor(Color.RED);
}
}
在您Preference.xml
需要创建PreferenceCategory
并Preference
使用这些自定义类。
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CustomPreferenceCategory android:title="@string/manage_device_title"
android:key="@string/device_category">
<CustomPreference android:title="@string/manage_device"
android:key="@string/manage_device_KEY"
android:summary="@string/device_summary" >
</CustomPreference>
</CustomPreferenceCategory>
</PreferenceScreen>
注意:在引用时请使用正确的包名 CustomPerenceCategory 和 CustomPreferencepreference.xml
并fontStyle
根据需要添加。