主界面的XML
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1" >        </FrameLayout>        <TabWidget            android:id="@android:id/tabs"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="0"            android:visibility="gone" >        </TabWidget>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="60dp" >            <RelativeLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1"                android:background="@drawable/tab_texture"                 android:id="@+id/rl_Cy">                <RadioButton                    android:id="@+id/rBtn_Cy"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerInParent="true"                    android:background="@null"                    android:button="@null"                    android:drawableTop="@drawable/cy_selector"                    android:gravity="center"                    android:text="@string/str_cy"                    android:textColor="@drawable/txtcol_selector"                    android:textSize="13sp" />            </RelativeLayout>            <RelativeLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1"                android:background="@drawable/tab_texture"                 android:id="@+id/rl_Aq">                <RadioButton                    android:id="@+id/rBtn_Aq"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerInParent="true"                    android:background="@null"                    android:button="@null"                    android:drawableTop="@drawable/aq_selector"                    android:gravity="center"                    android:text="@string/str_aq"                    android:textColor="@drawable/txtcol_selector"                    android:textSize="13sp" />            </RelativeLayout>            <RelativeLayout                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_weight="1"                android:background="@drawable/tab_texture"                 android:id="@+id/rl_Ys">                <RadioButton                    android:id="@+id/rBtn_Ys"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_centerInParent="true"                    android:background="@null"                    android:button="@null"                    android:drawableTop="@drawable/ys_selector"                    android:gravity="center"                    android:text="@string/str_ys"                    android:textColor="@drawable/txtcol_selector"                    android:textSize="13sp" />            </RelativeLayout>                    </LinearLayout>    </LinearLayout></TabHost>

主界面,Java代码
package com.example.test_x360;import android.os.Bundle;import android.app.TabActivity;import android.content.Intent;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RelativeLayout;import android.widget.TabHost;import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends TabActivity implementsOnCheckedChangeListener {private RelativeLayout rl_Cy;private RelativeLayout rl_Aq;private RelativeLayout rl_Ys;private RadioButton rBtn_Cy;private RadioButton rBtn_Aq;private RadioButton rBtn_Ys;// 定义标签名private static final String TAB_TAG_CY = "cyTag";private static final String TAB_TAG_AQ = "aqTag";private static final String TAB_TAG_YS = "ysTag";private TabHost mHost;private Intent cyIntent;private Intent aqIntent;private Intent ysIntent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setContent();setTabs();}private void initView() {rl_Cy = (RelativeLayout) findViewById(R.id.rl_Cy);rl_Aq = (RelativeLayout) findViewById(R.id.rl_Aq);rl_Ys = (RelativeLayout) findViewById(R.id.rl_Ys);rBtn_Cy = (RadioButton) findViewById(R.id.rBtn_Cy);rBtn_Aq = (RadioButton) findViewById(R.id.rBtn_Aq);rBtn_Ys = (RadioButton) findViewById(R.id.rBtn_Ys);rBtn_Cy.setOnCheckedChangeListener(this);rBtn_Aq.setOnCheckedChangeListener(this);rBtn_Ys.setOnCheckedChangeListener(this);}/**设置标签对应内容*/private void setContent() {cyIntent = new Intent(MainActivity.this, CyActivity.class);aqIntent = new Intent(MainActivity.this, AqActivity.class);ysIntent = new Intent(MainActivity.this, YsActivity.class);}private void setTabs() {// 获取TabHost对象mHost = getTabHost();// 添加TabSpecmHost.addTab(mHost.newTabSpec(TAB_TAG_CY).setContent(cyIntent).setIndicator(""));mHost.addTab(mHost.newTabSpec(TAB_TAG_AQ).setContent(aqIntent).setIndicator(""));mHost.addTab(mHost.newTabSpec(TAB_TAG_YS).setContent(ysIntent).setIndicator(""));// 设置默认显示标签mHost.setCurrentTabByTag(TAB_TAG_CY);// 设置默认选中按钮rBtn_Cy.setChecked(true);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {switch (buttonView.getId()) {case R.id.rBtn_Cy:if (isChecked) {//显示当前点击项对应的标签内容mHost.setCurrentTabByTag(TAB_TAG_CY);//其余的RadioButton选中状态清除rBtn_Aq.setChecked(false);rBtn_Ys.setChecked(false);rl_Cy.setBackgroundResource(R.drawable.tab_pressed);} else {rl_Cy.setBackgroundResource(R.drawable.tab_texture);}break;case R.id.rBtn_Aq:if (isChecked) {mHost.setCurrentTabByTag(TAB_TAG_AQ);rBtn_Cy.setChecked(false);rBtn_Ys.setChecked(false);rl_Aq.setBackgroundResource(R.drawable.tab_pressed);} else {rl_Aq.setBackgroundResource(R.drawable.tab_texture);}break;case R.id.rBtn_Ys:if (isChecked) {mHost.setCurrentTabByTag(TAB_TAG_YS);rBtn_Aq.setChecked(false);rBtn_Cy.setChecked(false);rl_Ys.setBackgroundResource(R.drawable.tab_pressed);} else {rl_Ys.setBackgroundResource(R.drawable.tab_texture);}break;}}}

更多相关文章

  1. Android自定义九宫格图案解锁
  2. 开发中常用到的几处代码设置
  3. android button自定义触摸前以及触摸时的样式
  4. ProgressDialog
  5. 使用Android(安卓)Studio时报错 Manifest merger failed...Sugge
  6. Android客户端GPS定位
  7. android点滴4
  8. android中设置控件获得焦点
  9. Mainfest

随机推荐

  1. Flutter底部导航栏BottomNavigationBar
  2. (20120722)(笔记004)android开发应用程序资源
  3. android两种方式中自己画一个圆,实现单点
  4. Android(安卓)drawable shape绘制边框
  5. Building Your First App(创建你的第一个
  6. layout_gravity 和 gravity以及对应值的
  7. android自学笔记(1):android简介
  8. Android基础复习
  9. android4.4 安装微信7.0.6版本底部显示黑
  10. 大量正规厂商进驻Android Market,请国人务