[Android]使用attrs新增物件的屬性並對應selector

功能描述:android的原生物件擁有自己的屬性,例如在layout.xml中設定一個TextView,便有text、enable之類的屬性,若自己想要設定android沒有提供的屬性,就需要在attrs裡面設定。

res/values/attrs.xml

  <declare-styleable name="CalendarPickerView">
    <attr name="android:background"/>
    <attr name="dividerColor" format="color"/>
    <attr name="dayBackground" format="reference"/>
    <attr name="dayTextColor" format="color"/>
    <attr name="titleTextColor" format="color"/>
    <attr name="displayHeader" format="boolean"/>
    <attr name="headerTextColor" format="color"/>
  </declare-styleable>
  <declare-styleable name="calendar_cell">
    <attr name="state_is_can_reserve" format="boolean" />
  </declare-styleable>

format的類別很多color、reference、boolean…
這邊要講的是boolean的控制對應selector

建立一個CustomeView 繼承 TextView

public class CustomeView extends TextView {
    private static final int[] STATE_CAN_RESERVE = {
          R.attr.state_is_can_reserve
      };
    private boolean isReserve = false;
    public void setReserve(boolean isReserve) {
        this.isReserve = isReserve;
        refreshDrawableState();
    }
    @Override protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isReserve) {
            mergeDrawableStates(drawableState, STATE_CAN_RESERVE);
        }
    }
}

super.onCreateDrawableState(extraSpace + 1)
extraSpace是指android的屬性數量,因為有新增一項自定義的,所以加1
mergeDrawableStates(drawableState, STATE_CAN_RESERVE)將自定義的屬性merge進原本的
refreshDrawableState()在setReserve時就會做狀態及畫面的refresh
這樣便完成屬性的設定

再來是selector

    public CalendarPickerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Resources res = context.getResources();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CalendarPickerView);
        final int bg = a.getColor(R.styleable.CalendarPickerView_android_background,
        res.getColor(R.color.calendar_bg));
        dividerColor = a.getColor(R.styleable.CalendarPickerView_dividerColor,
        res.getColor(R.color.calendar_divider));
        dayBackgroundResId = a.getResourceId(R.styleable.CalendarPickerView_dayBackground,
        R.drawable.calendar_bg_selector);
        dayTextColorResId = a.getResourceId(R.styleable.CalendarPickerView_dayTextColor,
        R.color.calendar_text_selector);
        titleTextColor = a.getColor(R.styleable.CalendarPickerView_titleTextColor,
        res.getColor(R.color.calendar_text_active));
        displayHeader = a.getBoolean(R.styleable.CalendarPickerView_displayHeader, true);
        headerTextColor = a.getColor(R.styleable.CalendarPickerView_headerTextColor,
        res.getColor(R.color.calendar_text_active));
    a.recycle();
    }

context.objainStyledAttributes(attrs, R.styleable.CalendarPickerView)
將attrs用TypeArray帶入程式,R.styleable.CalendarPickerView和attrs中的name要相同
dayTextColorResId = a.getResourceId(R.styleable.CalendarPickerView_dayTextColor,
R.color.calendar_text_selector);
將calendar_text_selector帶入

再來是selector

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:state_selected="true" android:color="@color/custom_text_selected"/>
    <item android:state_pressed="true" android:color="@color/custom_text_selected"/>
    <item app:state_current_month="false" android:color="@color/custom_text_inactive"/>
    <item app:state_today="true" android:color="@color/custom_text_selected"/>
    <item app:state_selectable="false" android:color="@color/custom_text_inactive" />
    <item
        app:state_current_month="true"
        app:state_is_can_reserve="true"
        android:color="@color/custom_background_today" />
    <item
        app:state_current_month="true"
        app:state_is_can_reserve="false"
        android:color="@color/custom_text_inactive" />
    <item android:color="@color/custom_background_today"/>
</selector>

此時,須先加入一行
xmlns:app="http://schemas.android.com/apk/res-auto"
這樣才能找到自定義的屬性
item 中的判斷很像if else 的判斷,如上,當兩個都true時才設定color

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *