Android_RadioGroup实现Tab
效果图:
代码:
一.布局文件
Android:button="@null" 设置RadioGroup的圆圈消失
android:drawableTop="@android:drawable/btn_star" 设置图片在文字的上面
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:background="#006644" >
- <RadioGroup
- android:id="@+id/rg"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <RadioButton
- android:id="@+id/rb_message"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:checked="true"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="消息" />
- <RadioButton
- android:id="@+id/rb_contact"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="联系人" />
- <RadioButton
- android:id="@+id/rb_dynamic"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="动态" />
- <RadioButton
- android:id="@+id/rb_setting"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:button="@null"
- android:drawableTop="@android:drawable/btn_star"
- android:gravity="center"
- android:text="设置" />
- </RadioGroup>
- </LinearLayout>
- </RelativeLayout>
二.设置按钮的选中事件监听器
- package com.example.fragment;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- public class MainActivity extends Activity {
- protected static final String TAG = "MainActivity";
- private RadioGroup rg;
- private RadioButton rb_message;
- private RadioButton rb_contact;
- private RadioButton rb_dynamic;
- private RadioButton rb_setting;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- // RadioGroup的点击事件
- rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup arg0, int arg1) {
- if (arg1 == rb_message.getId()) {
- Log.i(TAG, "选中了消息的按钮");
- } else if (arg1 == rb_contact.getId()) {
- Log.i(TAG, "选中了联系人的按钮");
- } else if (arg1 == rb_dynamic.getId()) {
- Log.i(TAG, "选中了动态的按钮");
- } else if (arg1 == rb_setting.getId()) {
- Log.i(TAG, "选中了设置的按钮");
- }
- }
- });
- }
- /**
- * 控件的实例化
- */
- private void init() {
- rg = (RadioGroup) findViewById(R.id.rg);
- rb_message = (RadioButton) findViewById(R.id.rb_message);
- rb_contact = (RadioButton) findViewById(R.id.rb_contact);
- rb_dynamic = (RadioButton) findViewById(R.id.rb_dynamic);
- rb_setting = (RadioButton) findViewById(R.id.rb_setting);
- }
- }

浙公网安备 33010602011771号