0

クラスをサブクラス化してSpinnerいます.xmlファイルで指定されたオブジェクトを取得する必要がありますandroid:entries. どうやってやるの?

<com.myapp.MySpinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/myarray"/>

そのため、引数で指定された配列か、entries自分で取得できるようにリソース ID が必要です。

4

1 に答える 1

1

コンストラクターをオーバーライドし、 paramSpinnerからエントリを取得する必要があります。AttributeSet

public class MySpinner extends Spinner {
    public MySpinner(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (attrs != null) {
            final int[] ids = {android.R.attr.entries};
            final TypedArray array = context.obtainStyledAttributes(attrs, ids);
            final int entriesId = array.getResourceId(0, 0);

            if (entriesId > 0) {
                final Resources resources = context.getResources();
                final String[] entries = resources.getStringArray(entriesId);

                if (entries != null) {
                    // do whatever you want here
                }
            }
        }
    }
}
于 2013-02-17T12:37:17.033 に答える