我做了CustomPreference,它适用于PreferenceCategory,但CustomPreferenceCategory有一个问题,比如uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceCategory不能转换为android.preference.GenericInflater$Parent
CustomPreferenceCategory.class
public class CustomPreferenceCategory extends PreferenceCategory {
private TextView txt;
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWidgetLayoutResource(R.layout.preference_category_title);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
txt = (TextView) view.findViewById(R.id.text_view_preference_category_title);
}
public void setText(String text, int color) {
txt.setText(text);
txt.setTextColor(color);
}
}
偏好类别标题
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey_231"
>
<TextView
android:id="@+id/text_view_preference_category_title"
android:layout_width="wrap_content"
android:layout_height="32.5dp"
android:text="@string/profile_preference"
android:textSize="14dp"
android:gravity="center"
app:layout_marginLeftPercent="3.5%"
android:textColor="@color/charcoalGrey_30"
/>
</android.support.percent.PercentRelativeLayout>
偏好.xml
<uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceCategory
android:name="uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceCategory"
android:key="@string/profile_preference"
android:layout="@layout/preference_category_title">
<uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceButton
android:name="uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceButton"
android:layout="@layout/preference_button"
android:key="@string/edit_profil_button_preference"/>
<uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceButton
android:key="@string/change_password_preference"
android:layout="@layout/preference_button"/>
</uk.co.bla.bla.customwidget.viewgroup.CustomPreferenceCategory>