我正在关注此android developer page以创建应用程序的自定义属性。一切都很顺利,我能够编译并看到结果。 但是问题的部分是IDE.Android Studio正在抱怨意外的命名空间自定义。有什么办法可以禁用/隐藏这个错误信息吗?这非常烦人。尝试使用自定义属性时发现标记片段的意外名称空间前缀“自定义”
但是,即使有这个错误,我也能编译和运行应用程序。
我跟着follwing步骤。
1)创建RES /价值/ attrs.xml文件,并添加
<?xml version="1.0" encoding="utf-8"?> <resources>
<declare-styleable name="FragArguments">
<attr name="max_allowed" format="integer"/>
<attr name="header" format="string"/>
</declare-styleable>
2)想在我mainlayout文件的片段标签
<LinearLayout xmlns:custom="http://schemas.android.com/apk/res"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<fragment android:id="@+id/msg"
android:name="org.android.fragment.MessageFragment"
custom:max_allowed="85"
custom:header="@string/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
3.应用使用自定义属性通过代码覆盖片段的onInflate()方法
@Override
public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
super.onInflate(activity, attrs, savedInstanceState);
TypedArray typedArray = activity.obtainStyledAttributes(attrs, R.styleable.FragArguments);
max_allowed = typedArray.getInt(R.styleable.FragArguments_max_allowed, -1);
header = typedArray.getString(R.styleable.FragArguments_header);
typedArray.recycle();
}