Nic*_*cks 1 android-layout android-studio android-custom-attributes
我正在关注这个Android开发者页面,以便为应用程序创建自定义属性.一切都很顺利,我也可以编译并查看结果.但是提出问题的部分是IDE.Android Studio抱怨意外的命名空间自定义.有没有办法禁用/隐藏此错误消息?这非常烦人.
但是,即使出现此错误,我也可以编译并运行该应用程序.

我遵循了以下步骤.
1)创建res/values/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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
我刚刚想出了解决方案.
该问题ID MissingPrefix告诉皮棉只扫描那些丢失了Android命名空间prefix.So我们能做到这两个方面XML属性: -
lint --check MissingPrefix myproject
Run Code Online (Sandbox Code Playgroud)<?xml version="1.0" encoding="utf-8"?> <lint> <issue id="MissingPrefix" severity="ignore"/> </lint>
| 归档时间: |
|
| 查看次数: |
1156 次 |
| 最近记录: |