android之RadioGroup

时间:2021-09-10 16:35:36

radioGroup这控件在开发中也是常用到的

RadioGroup 有时候比较有用.主要特征是给用户提供多选一机制。

用微信举一个例子吧!

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:background="#383A37"
> <TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="18sp"
android:text="@string/weixin" /> <TextView
android:id="@+id/textView2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/blank" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal"> <ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center_vertical"
android:src="@drawable/fdj"
android:layout_marginRight="10dp"/> <ImageView
android:id="@+id/imageView2"
android:layout_width="30dp"
android:layout_height="30dp"
android:gravity="center_vertical"
android:src="@drawable/barbuttonicon_add"
android:layout_marginRight="10dp" />
</LinearLayout> </LinearLayout>

head.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
> <RadioButton
android:id="@+id/radio0"
android:checked="true"
style="@style/radioStyle"
android:drawableTop="@drawable/radio_weixin_check"
android:textColor="@drawable/radio_color_check"
android:text="@string/weixin" /> <RadioButton
android:id="@+id/radio1"
style="@style/radioStyle"
android:drawableTop="@drawable/radio_tongxunlu_check"
android:textColor="@drawable/radio_color_check"
android:text="@string/tongxunlu" /> <RadioButton
android:id="@+id/radio2"
style="@style/radioStyle"
android:drawableTop="@drawable/radio_faxian_check"
android:textColor="@drawable/radio_color_check"
android:text="@string/faxian" /> <RadioButton
android:id="@+id/radio3"
style="@style/radioStyle"
android:drawableTop="@drawable/radio_wo_check"
android:textColor="@drawable/radio_color_check"
android:text="@string/wo" />
</RadioGroup> </LinearLayout>

下面是微信底部的文件

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:color="#A0A0A0"></item>
<item android:state_checked="true" android:color="#45C01b"></item>
</selector>
 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@drawable/tabbar_discover"></item>
<item android:state_checked="true" android:drawable="@drawable/tabbar_discoverhl"></item>
</selector>

后三个同上就不展开了

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@drawable/tabbar_mainframe"></item>
<item android:state_checked="true" android:drawable="@drawable/tabbar_mainframehl"></item>
</selector>

radio_weixin_check

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@drawable/tabbar_me"></item>
<item android:state_checked="true" android:drawable="@drawable/tabbar_mehl"></item>
</selector>

radio_wo_check

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@drawable/tabbar_contacts"></item>
<item android:state_checked="true" android:drawable="@drawable/tabbar_contacts_hl"></item>
</selector>

radio_tongxunlu_check

mainXML配置

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.main.weixin.MainActivity" > <!-- 头部 -->
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
> <include layout="@layout/head"/> </LinearLayout> <!-- 中间 -->
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"> <include layout = "@layout/listview_layout"></include> </LinearLayout> <!-- 尾部 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
> <include layout="@layout/bottom"/> </LinearLayout>
</LinearLayout>

效果如下

android之RadioGroup