2012-02-17 63 views
0

我已经扩展了PreferenceCategory类。它的工作原理除了Android属性“标题”没有被传递并显示在屏幕上。任何帮助,将不胜感激。这里是我的代码:如何扩展PreferenceCategory类

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

您需要使用的AttributeSet拿到冠军:

mTitle = attrs.getAttributeValue(androidns, "title"); 

其中androidns是: 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..... 

} 
+0

大:)我现在有标题。 (我用新的问题和新的代码更新了问题。)我如何将标题设置到View中?你能帮助我吗? – 2012-02-17 14:55:35