选项卡片段
16lz
2021-12-04
主界面的XML
主界面,Java代码
<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;}}}
更多相关文章
- Android自定义九宫格图案解锁
- 开发中常用到的几处代码设置
- android button自定义触摸前以及触摸时的样式
- ProgressDialog
- 使用Android(安卓)Studio时报错 Manifest merger failed...Sugge
- Android客户端GPS定位
- android点滴4
- android中设置控件获得焦点
- Mainfest