アプリのカスタム属性を作成するために、この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>
2) メインレイアウト ファイルのフラグメント タグで使用しようとしています
<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();
}