Android_RadioGroup实现Tab

效果图:

 

代码:

一.布局文件

 

 Android:button="@null"  设置RadioGroup的圆圈消失

 android:drawableTop="@android:drawable/btn_star"   设置图片在文字的上面

 

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_alignParentBottom="true"  
  10.         android:background="#006644" >  
  11.   
  12.         <RadioGroup  
  13.             android:id="@+id/rg"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:orientation="horizontal" >  
  17.   
  18.             <RadioButton  
  19.                 android:id="@+id/rb_message"  
  20.                 android:layout_width="0dp"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:layout_weight="1"  
  23.                 android:button="@null"  
  24.                 android:checked="true"  
  25.                 android:drawableTop="@android:drawable/btn_star"  
  26.                 android:gravity="center"  
  27.                 android:text="消息" />  
  28.   
  29.             <RadioButton  
  30.                 android:id="@+id/rb_contact"  
  31.                 android:layout_width="0dp"  
  32.                 android:layout_height="wrap_content"  
  33.                 android:layout_weight="1"  
  34.                 android:button="@null"  
  35.                 android:drawableTop="@android:drawable/btn_star"  
  36.                 android:gravity="center"  
  37.                 android:text="联系人" />  
  38.   
  39.             <RadioButton  
  40.                 android:id="@+id/rb_dynamic"  
  41.                 android:layout_width="0dp"  
  42.                 android:layout_height="wrap_content"  
  43.                 android:layout_weight="1"  
  44.                 android:button="@null"  
  45.                 android:drawableTop="@android:drawable/btn_star"  
  46.                 android:gravity="center"  
  47.                 android:text="动态" />  
  48.   
  49.             <RadioButton  
  50.                 android:id="@+id/rb_setting"  
  51.                 android:layout_width="0dp"  
  52.                 android:layout_height="wrap_content"  
  53.                 android:layout_weight="1"  
  54.                 android:button="@null"  
  55.                 android:drawableTop="@android:drawable/btn_star"  
  56.                 android:gravity="center"  
  57.                 android:text="设置" />  
  58.         </RadioGroup>  
  59.     </LinearLayout>  
  60.   
  61.   
  62. </RelativeLayout>  



 

二.设置按钮的选中事件监听器

 

 

[java] view plain copy
 
    1. package com.example.fragment;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.util.Log;  
    6. import android.widget.RadioButton;  
    7. import android.widget.RadioGroup;  
    8. import android.widget.RadioGroup.OnCheckedChangeListener;  
    9.   
    10. public class MainActivity extends Activity {  
    11.   
    12.     protected static final String TAG = "MainActivity";  
    13.     private RadioGroup rg;  
    14.     private RadioButton rb_message;  
    15.     private RadioButton rb_contact;  
    16.     private RadioButton rb_dynamic;  
    17.     private RadioButton rb_setting;  
    18.   
    19.     @Override  
    20.     protected void onCreate(Bundle savedInstanceState) {  
    21.         super.onCreate(savedInstanceState);  
    22.         setContentView(R.layout.activity_main);  
    23.         init();  
    24.   
    25.         // RadioGroup的点击事件  
    26.         rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
    27.   
    28.             @Override  
    29.             public void onCheckedChanged(RadioGroup arg0, int arg1) {  
    30.   
    31.                 if (arg1 == rb_message.getId()) {  
    32.   
    33.                     Log.i(TAG, "选中了消息的按钮");  
    34.   
    35.                 } else if (arg1 == rb_contact.getId()) {  
    36.   
    37.                     Log.i(TAG, "选中了联系人的按钮");  
    38.   
    39.                 } else if (arg1 == rb_dynamic.getId()) {  
    40.   
    41.                     Log.i(TAG, "选中了动态的按钮");  
    42.   
    43.                 } else if (arg1 == rb_setting.getId()) {  
    44.   
    45.                     Log.i(TAG, "选中了设置的按钮");  
    46.   
    47.                 }  
    48.   
    49.             }  
    50.         });  
    51.   
    52.     }  
    53.   
    54.     /** 
    55.      * 控件的实例化 
    56.      */  
    57.     private void init() {  
    58.   
    59.         rg = (RadioGroup) findViewById(R.id.rg);  
    60.         rb_message = (RadioButton) findViewById(R.id.rb_message);  
    61.         rb_contact = (RadioButton) findViewById(R.id.rb_contact);  
    62.         rb_dynamic = (RadioButton) findViewById(R.id.rb_dynamic);  
    63.         rb_setting = (RadioButton) findViewById(R.id.rb_setting);  
    64.   
    65.     }  
    66.   
    67. }  
posted @ 2017-04-18 08:39  天涯海角路  阅读(159)  评论(0)    收藏  举报