我扩展了PreferenceCategory类。它的工作原理除了Android属性“title”没有被传递并在屏幕上显示。任何帮助,将不胜感激。这是我的代码:
XML \ preferences_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="advanced_settings_screen">
<apps.test.CustomPreferenceCategory
android:key="advanced_general_settings_category"
android:title="@string/general_category">
<!-- Other Preferences Here -->
</apps.test.CustomPreferenceCategory>
</PreferenceScreen>
CustomPreferenceCategory.java
public class CustomPreferenceCategory extends PreferenceCategory {
public CustomPreferenceCategory(Context context, AttributeSet attrs) {
super(context, attrs);
Log.v("Constructor 1");
this.setLayoutResource(R.layout.custom_preference_category);
String titleAttribute = _attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "title");
if(titleAttribute.startsWith("@")){
titleAttribute = _context.getString(Integer.parseInt(titleAttribute.replace("@", "")));
}
//What can I do here to populate the custom view with the title? I can't seem to figure this part out.
}
public CustomPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Log.v("Constructor 2");
this.setLayoutResource(R.layout.custom_preference_category);
}
}
布局\ custom_preference_category.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingTop="25dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:text="TEST" />
<ImageView
android:id="@+id/category_divider"
android:src="@drawable/preference_divider_normal"
android:layout_height="2dip"
android:layout_width="fill_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
</LinearLayout>
我只想让属性“TITLE”通过并显示在屏幕上。我能够得到“标题”attirbute,但现在我似乎无法弄清楚如何将标题设置为自定义视图。任何人都可以帮忙吗?
由于
答案 0 :(得分:0)
您需要使用AttributeSet来获取标题:
mTitle = attrs.getAttributeValue(androidns, "title");
安卓的地方是:
private static final String androidns = "http://schemas.android.com/apk/res/android";
编辑:添加了有关使用标题的信息....
将您的布局添加到xml中的首选项:
android:layout="@layout/profile_preference_row"
现在你需要覆盖onBindView(android源代码下面的示例)......
@Override
protected void onBindView(View v) {
super.onBindView(v);
TextView textView = (TextView) view.findViewById(R.id.title);
if (textView != null) {
textView.setText(getTitle());
.......removed code.....
}