2011-12-27 47 views
28

我想在自定義控件中使用XML(不使用捆綁軟件附加參數)像declare-styleable這樣在Android碎片中定義自定義屬性。但是沒有帶AttrSet參數的構造函數,所以有可能嗎?我是否可以覆蓋public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState)以獲得屬性支持?Android碎片中的自定義屬性

+0

得到錯誤而編譯:'... \程序\資源\佈局\ select_category.xml:26:錯誤:未找到屬性「showRadioButtons資源標識符'in package'com.companyX.projectY' ... \ app \ res \ layout \ select_category.xml:26:錯誤:找不到包'com.companyX.projectY'中的屬性'highlightSelection'的資源標識符 .. 。app \ res \ layout \ select_category.xml:26:error:找不到屬性'unselectedColor'的包com.companyX.projectY''中的資源標識符 – Anton 2011-12-27 06:54:20

+0

app xml命名空間和declare-stylable被寫入corr ectly。可以提供它們,如果需要 – Anton 2011-12-27 06:57:12

回答

1
@Override 
public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { 
    super.onInflate(activity, attrs, savedInstanceState); 
    // Your code here to process the attributes 
} 
+2

工程,但一定要清除棉絨標記(Android工具>清除棉絨標記)。我花了1000萬美元試圖弄清楚爲什麼它沒有建設! – tdevaux 2013-08-11 00:28:27

+0

感謝tdevaux,我只花了5分鐘才重新閱讀本文。我的生活中還有5分鐘可用! – dhaag23 2014-05-15 01:18:50

71

Link for Support4Demos已更改或可以更改,因此發佈了完整的解決方案。在這裏。

  1. 創建attrs.xml文件在res /值的文件夾。或者如果文件已經存在,則添加以下內容。在它

    /** 
    * Parse attributes during inflation from a view hierarchy into the 
    * arguments we handle. 
    */ 
    @Override 
    public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { 
        super.onInflate(activity, attrs, savedInstanceState); 
        Log.v(TAG,"onInflate called"); 
    
        TypedArray a = activity.obtainStyledAttributes(attrs,R.styleable.MyFragment); 
    
        CharSequence myString = a.getText(R.styleable.MyFragment_my_string); 
        if(myString != null) { 
         Log.v(TAG, "My String Received : " + myString.toString()); 
        } 
    
        int myInteger = a.getInt(R.styleable.AdFragment_my_integer, -1); 
        if(myInteger != -1) { 
         Log.v(TAG,"My Integer Received :" + myInteger); 
        } 
    
        a.recycle(); 
    } 
    
  2. <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
    <declare-styleable name="MyFragment"> 
        <attr name="my_string" format="string"/> 
        <attr name="my_integer" format="integer"/> 
    </declare-styleable> 
    

  3. 覆蓋的片段onInflate代表和讀取屬性傳遞到自己的佈局文件這些屬性如下。只是一個例子

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 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="match_parent" > 
    
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="This is android activity" /> 
    
        <fragment 
         android:id="@+id/ad_fragment" 
         android:name="com.yourapp.packagename.MyFragment" 
         android:layout_width="fill_parent" 
         android:layout_height="50dp" 
         android:layout_alignParentBottom="true" 
         app:my_string="Hello This is HardCoded String. Don't use me" 
         app:my_integer="30" /> 
    
    </RelativeLayout> 
    

這就是所有。它的工作解決方案。

雖然這樣做,如果您在xml中看到任何命名空間錯誤。 嘗試項目清潔一次又一次。 這是可悲的,但日食和adt有時行爲不端。

希望它可以幫助別人:)

乾杯

+2

我的Android Studio不斷用紅色下劃線在XML中顯示自定義屬性,表明存在錯誤,但整個項目構建得很好。供參考的人可能認爲他們沒有正確地在他們的佈局文件中查看這些錯誤。 – Pocha 2014-11-04 06:37:23

+1

你可以使用'http:// schemas.android.com/apk/res-auto'來代替xmlns:app =「http://schemas.android.com/apk/res/com.yourapp.packagename」來自動 - 替代包名稱。 http://stackoverflow.com/questions/10448006/xml-namespace-declaration-auto-substitute-package-name – 2014-11-18 02:06:45

+0

好吧我以前錯過了那一個。如果你像我一樣從副本上粘貼了這些內容,請注意並確保命名空間匹配,例如** xmlns:$ {NameSpace} **和屬性** $ {NameSpace}中的相同**:attribute =「...」 – TacB0sS 2014-11-27 21:43:55