2011-12-27 51 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