2012-02-17 22 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