1

ライブラリ アプリケーション内にカスタム複合ビューを作成しましたが、すべて問題ありませんでした。ビューにカスタム属性を追加すると、常にデフォルト値が取得されます。この手順に従って、 1 つだけ違いがあります。私のビューはライブラリ プロジェクトにあります。

/res/values/attrs.xml

<resources>
    <declare-styleable name="DatePickerView">
        <attr name="showToday" format="boolean" />
        <attr name="calendar" format="enum">
            <enum name="jalali" value="0" />
            <enum name="gregorian" value="1" />
        </attr>
    </declare-styleable>
</resources>

ビューを含むレイアウト ファイル:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:farayan="http://schemas.android.com/apk/lib/net.farayan.android.view"
...
    <net.farayan.android.view.datepicker.DatePickerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        farayan:showToday="false"
        farayan:calendar="gregorian"/>
...

コンポーネントのコード:

int calendar;
boolean showToday;

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DatePickerView, 0, 0);
try {
    calendar = a.getInteger(R.styleable.DatePickerView_calendar, 0);
    showToday = a.getBoolean(R.styleable.DatePickerView_showToday, true);
} finally {
    a.recycle();
}

calendarそしてshowToday常に0そしてtrueそれぞれです。何か案が?

4

2 に答える 2

0

ここに何か問題があるようです:

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:farayan="http://schemas.android.com/apk/lib/net.farayan.android.view"

次のように変更します。

        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:farayan="http://schemas.android.com/apk/res/net.farayan.android.view"

net.farayan.android.viewアプリのルート名前空間です。

アップデート1

xmlns:farayan="http://schemas.android.com/apk/res/your_main_app_package"

ここに例があります。ライブラリ プロジェクトで定義されたビューを使用します。

于 2013-07-21T09:51:37.920 に答える
0

プロジェクト内に新しい複合ビュー コードとその属性を追加する場合は、これをレイアウトの先頭に追加する必要があります。

xmlns:custom="http://schemas.android.com/apk/res/your_main_app_package

新しい複合ビューが peoject にリンクされたライブラリ プロジェクト内にある場合は、これを追加する必要があります。

xmlns:custom="http://schemas.android.com/apk/res-auto

リンク: https://stackoverflow.com/a/10217752/1152549

于 2014-03-11T07:40:33.660 に答える