http://download.csdn.net/detail/sun6223508/3454092
作为android 程序员来说,TabHost是最常用的控件。前面介绍过ActivityGroup,它做出来的效果和tabhost是一样的。只不过ActivityGroup是一个activity容器。
今天给大家介绍RadioGroup,用RadioGroup实现tabhost的效果。
MainActivity.java
public class MainActivity extends TabActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
private TabHost mHost;
private Intent first;
private Intent second;
private Intent third;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainview);
this.first = new Intent(this, test1.class);
this.second = new Intent(this, test1.class);
this.third = new Intent(this, test2.class);
initRadios();
setupIntent();
}
private void setupIntent() {
this.mHost = getTabHost();
TabHost localTabHost = this.mHost;
localTabHost.addTab(buildTabSpec("mblog_tab", R.string.hello,
R.drawable.icon_1_n, this.first));
localTabHost.addTab(buildTabSpec("message_tab", R.string.hello,
R.drawable.icon_2_n, this.second));
localTabHost.addTab(buildTabSpec("userinfo_tab", R.string.hello,
R.drawable.icon_3_n, this.third));
}
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
Intent content) {
return this.mHost.newTabSpec(tag).setIndicator(getString(resLabel),
getResources().getDrawable(resIcon)).setContent(content);
}
private void initRadios() {
((RadioButton) findViewById(R.id.titleat))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.titlecomment))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.titlemessage))
.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
switch (buttonView.getId()) {
case R.id.titleat:
Log.v("buildTabSpec", "");
this.mHost.setCurrentTabByTag("mblog_tab");
break;
case R.id.titlecomment:
Log.v("buildTabSpec", "");
this.mHost.setCurrentTabByTag("message_tab");
break;
case R.id.titlemessage:
Log.v("buildTabSpec", "");
this.mHost.setCurrentTabByTag("userinfo_tab");
break;
}
}
}
}
main.xml
<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:gravity="center_horizontal"
android:layout_gravity="top"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/titlebar_lightgray_bg">
<RadioGroup android:layout_gravity="top"
android:layout_centerVertical="true" android:gravity="center_horizontal"
android:layout_centerHorizontal="true" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton android:id="@+id/titleat"
android:layout_marginTop="2.0dip" android:text="sun"
android:background="@drawable/title_button_group_left" style="@style/main_tab_bottom" />
<RadioButton android:id="@+id/titlecomment"
android:layout_marginTop="2.0dip" android:text="6223508"
android:background="@drawable/title_button_group_middle" style="@style/main_tab_bottom" />
<RadioButton android:id="@+id/titlemessage"
android:layout_marginTop="2.0dip" android:text="gmail"
android:background="@drawable/title_button_group_right" style="@style/main_tab_bottom" />
</RadioGroup>
</RelativeLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0.0" />
<TabWidget android:id="@android:id/tabs" android:visibility="gone"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1.0" />
</LinearLayout>
</TabHost>
test1.java 和test2.java的内容是一样的。test2.java这里就不给大家贴了
public class test1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:background="@drawable/sfsd"
/>
</LinearLayout>
styles.xml
<style name="main_tab_bottom">
<item name="android:textSize">@dimen/bottom_tab_font_size</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/home_btn_bg</item>
<item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
<item name="android:layout_weight">1.0</item>
</style>