【问题标题】:Custom attributes in Android fragmentsAndroid 片段中的自定义属性
【发布时间】:2012-01-28 07:40:40
【问题描述】:

我想在自定义控件中使用 XML(不使用捆绑附加参数)在 Android 片段中定义自定义属性,例如 declare-styleable。但是没有带有 AttrSet 参数的构造函数,那么有可能吗?我可以覆盖public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState) 以获得属性支持吗?

【问题讨论】:

  • 编译时出错:...\app\res\layout\select_category.xml:26: error: No resource identifier found for attribute 'showRadioButtons' in package 'com.companyX.projectY' ...\app\res\layout\select_category.xml:26: error: No resource identifier found for attribute 'highlightSelection' in package 'com.companyX.projectY' ...\app\res\layout\select_category.xml:26: error: No resource identifier found for attribute 'unselectedColor' in package 'com.companyX.projectY'
  • app xml 命名空间和 declare-stylable 编写正确。如果需要,可以提供他们

标签: android android-fragments custom-attributes fragment


【解决方案1】:

Support4Demos 的链接已更改或可以更改,因此发布完整的解决方案。就这样吧。

  1. 在 res/values 文件夹中创建 attrs.xml 文件。或者如果文件已经存在,则添加以下内容。

    <?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> 
    

  2. 覆盖片段的 onInflate 委托并读取其中的属性

    /**
     * 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();
    }
    
  3. 在您的布局文件中传递这些属性,如下所示。只是一个例子

    <?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 中看到任何命名空间错误。 一次又一次地尝试项目清理。 这很可悲,但有时 eclipse 和 adt 行为不端。

希望它可以帮助其他人:)

干杯

【讨论】:

  • 我的 Android Studio 一直在 XML 中显示自定义属性,红色下划线表示存在错误,但整个项目构建得很好。仅供参考,对于那些可能认为他们没有正确查看布局文件中的错误的人。
  • 您可以使用http://schemas.android.com/apk/res-auto 代替 xmlns:app="schemas.android.com/apk/res/com.yourapp.packagename" 来自动替换包名。 stackoverflow.com/questions/10448006/…
  • 好的,我之前错过了那个。如果您像我一样从几个地方复制粘贴此内容,请注意并确保名称空间匹配,例如xmlns:${NameSpace} 和属性 ${NameSpace}:attribute="..." 相同
  • Android Studio 显示 onInflate(Activity activity ... 已弃用。您可以使用这样的新处理程序(Kotlin):override fun onInflate(context: Context?, attrs: AttributeSet?, savedInstanceState: Bundle?) { super.onInflate(context, attrs, savedInstanceState) val styledAttributes = context?.obtainStyledAttributes(attrs, R.styleable.MyFragment_my_string) val text = styledAttributes?.getText(R.styleable.MyFragment_my_string) styledAttributes?.recycle() }
【解决方案2】:
@Override
public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
    super.onInflate(activity, attrs, savedInstanceState); 
    // Your code here to process the attributes
}

【讨论】:

  • 有效,但请确保清除 Lint 标记(Android 工具 > 清除 Lint 标记)。我花了 1000 万美元试图弄清楚为什么它没有建成!
  • 感谢 tdevaux,我只花了 5 分钟就重新阅读了这篇文章。我的生命还有 5 分钟可用!
猜你喜欢
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多