在项目中我较习惯于使用RadioButton来实现上方或者下方的TabBar,因为RadioButton和RadioGroup有一个非常好用的check属性。
可最近的项目中使用系统自带的RadioButton时却不知道为什么发生了内容不居中的问题,又不想每次写TabBar的时候都写一大堆的布局和监听,于是就想到了自定义。
为了让自定义的view能够使用selector,我还去参照了源码的写法。
最终实现了如下效果:
xml布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:radioButton="http://schemas.android.com/apk/res/com.example.checkableviewtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.checkableviewtest.CustomRadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:orientation="horizontal"
android:weightSum="4"
>
<com.example.checkableviewtest.CustomRadioButton
android:id="@+id/rb1"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
radioButton:checked="true"
radioButton:drawableWidth="20dip"
radioButton:drawableHeight="20dip"
radioButton:res="@drawable/selector_notice_list_item_checkbox"
radioButton:drawableGravity="top"
radioButton:text="ha1"
radioButton:textColor="@color/selector_main_tab"
radioButton:textSize="14sp"
>
</com.example.checkableviewtest.CustomRadioButton>
<ImageView
android:layout_width="1px"
android:layout_height="fill_parent"
android:background="#ff0000"
/>
<com.example.checkableviewtest.CustomRadioButton
android:id="@+id/rb2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
radioButton:drawableWidth="20dip"
radioButton:drawableHeight="20dip"
radioButton:res="@drawable/selector_notice_list_item_checkbox"
radioButton:drawableGravity="top"
radioButton:text="ha2"
radioButton:textColor="@color/selector_main_tab"
radioButton:textSize="14sp"
>
</com.example.checkableviewtest.CustomRadioButton>
<ImageView
android:layout_width="1px"
android:layout_height="fill_parent"
android:background="#ff0000"
/>
<com.example.checkableviewtest.CustomRadioButton
android:id="@+id/rb3"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
radioButton:drawableWidth="20dip"
radioButton:drawableHeight="20dip"
radioButton:res="@drawable/selector_notice_list_item_checkbox"
radioButton:drawableGravity="top"
radioButton:text="ha3"
radioButton:textColor="@color/selector_main_tab"
radioButton:textSize="14sp"
>
</com.example.checkableviewtest.CustomRadioButton>
<ImageView
android:layout_width="1px"
android:layout_height="fill_parent"
android:background="#ff0000"
/>
<com.example.checkableviewtest.CustomRadioButton
android:id="@+id/rb4"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
radioButton:drawableWidth="20dip"
radioButton:drawableHeight="20dip"
radioButton:res="@drawable/selector_notice_list_item_checkbox"
radioButton:drawableGravity="top"
radioButton:text="ha4"
radioButton:textColor="@color/selector_main_tab"
radioButton:textSize="14sp"
>
</com.example.checkableviewtest.CustomRadioButton>
</com.example.checkableviewtest.CustomRadioGroup>
</RelativeLayout>
项目地址 http://download.youkuaiyun.com/detail/neverwoods/8686639