您需要創建爲CustomPreference
和CustomPreferenceCategory
。包括在preference.xml
CustomPreference CustomPreference
和CustomPreferenceCategory
:
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);
}
}
CustomPreferenceCategory:
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>
注:請preference.xml
refereing時使用CustomPerenceCategory和CustomPreference適當的包名和fontStyle
加按的需要。
謝謝你的回答。對不起,我已經在我的代碼中使用preferenceScreen.addPreference()函數向child preference screen添加了首選項。當我使用CustomPreference時,此方法不可用。所以我不能將偏好添加到子屏幕。所以我該怎麼做? –
@KelumDeshapriya:你不是在使用'addPreferencesFromResource(R.xml.preference)'嗎? –
你好。我使用它。但我也使用addPreference()將我的動態首選項列表添加到程序中的child preferenceScreen。 –