我想在自定义控件中使用XML(不使用捆绑软件附加参数)像declare-styleable
这样在Android碎片中定义自定义属性。但是没有带AttrSet参数的构造函数,所以有可能吗?我是否可以覆盖public void onInflate(android.app.Activity activity, android.util.AttributeSet attrs, android.os.Bundle savedInstanceState)
以获得属性支持?Android碎片中的自定义属性
回答
@Override
public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
super.onInflate(activity, attrs, savedInstanceState);
// Your code here to process the attributes
}
Link for Support4Demos已更改或可以更改,因此发布了完整的解决方案。在这里。
创建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(); }
-
<?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>
覆盖的片段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有时行为不端。
希望它可以帮助别人:)
干杯
我的Android Studio不断用红色下划线在XML中显示自定义属性,表明存在错误,但整个项目构建得很好。供参考的人可能认为他们没有正确地在他们的布局文件中查看这些错误。 – Pocha 2014-11-04 06:37:23
你可以使用'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
好吧我以前错过了那一个。如果你像我一样从副本上粘贴了这些内容,请注意并确保命名空间匹配,例如** xmlns:$ {NameSpace} **和属性** $ {NameSpace}中的相同**:attribute =“...” – TacB0sS 2014-11-27 21:43:55
- 1. 自定义视图中的Android属性
- 2. Android中的自定义标记属性
- 3. android在自定义视图上并排定义属性和自定义属性
- 4. 的Android - 碎片 - 套装属性的片段
- 5. Android的碎片+自定义列表视图
- 6. 通行证自定义属性自定义片段着色器
- 7. 自定义类型的Android属性?
- 8. 自定义Android视图的自定义类型属性
- 9. 具有自定义属性的Android自定义视图
- 10. Android的自定义视图 - 自定义错误属性
- 11. Android的碎片
- 12. Android的碎片
- 13. 自定义属性
- 14. 自定义属性
- 15. 自定义属性
- 16. 自定义属性
- 17. 自定义属性
- 18. 自定义属性
- 19. 自定义属性
- 20. 在Eloquent中定义自定义属性
- 21. 在CIL中定义自定义属性
- 22. 自定义属性的属性
- 23. 引用属性的自定义属性
- 24. Android库和自定义xml属性
- 25. Android自定义属性不显示
- 26. Android自定义组件属性
- 27. Android单选按钮自定义属性
- 28. 自定义属性错误 - Android Studio 1.2
- 29. 如何在Android中定义自定义属性
- 30. Android和自定义视图和自定义XML属性
得到错误而编译:'... \程序\资源\布局\ 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
app xml命名空间和declare-stylable被写入corr ectly。可以提供它们,如果需要 – Anton 2011-12-27 06:57:12