有多个ViewGroup并列的情景中,我们需要选出一个,改变其背景色,区别于其它未选状态。
如图所示:
xml布局文件:
<LinearLayout android:id="@+id/vw_user_tab_video" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center" android:layout_marginBottom="3dp" android:layout_weight="1"> <TextView android:id="@+id/vw_user_tab_video_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="@color/user_tab_text_color" android:textSize="15sp" android:text="0"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="@color/user_tab_text_color" android:textSize="15sp" android:text="@string/user_tab_video"/> </LinearLayout>
这个时候,通常有两种方案。
方案1:通过单独设置组件中每一个文子的颜色,来区分开。但是如果ViewGroup包含多个子控件,工作量太大。
因此,要采用其它方案。
方案2:通过设置RelativeLayout/LinearLayout的selected状态来区分UI。
java代码:
/** * 选中指定tab * @param i */ public void setSelectTab(int i){ mVideoTab.setSelected(false); mZanTab.setSelected(false); mFollowTab.setSelected(false); mFansTab.setSelected(false); switch (i){ case TAB_VIDEO: mVideoTab.setSelected(true); break; case TAB_ZAN: mZanTab.setSelected(true); break; case TAB_FOLLOW: mFollowTab.setSelected(true); break; case TAB_FANS: mFansTab.setSelected(true); break; } }