选项卡片段

主界面的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 implements
OnCheckedChangeListener {
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;


@Override
protected 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();
// 添加TabSpec
mHost.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);
}

@Override
public 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;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值