转载至:新浪微博布局学习——妙用TabHost
注:TabWidget 布局里边放入各个Tab挺有意思的。这样做到即见即所得的效果。
<TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" />
<RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/maintab_toolbar_bg" android:layout_width="fill_parent" android:layout_height="wrap_content">
<RadioButton android:text="@string/main_home" android:checked="true" android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />
</RadioGroup>
</TabWidget>
如果遇到特殊的TabWidget布局可以采取这种方式,一般的TabWidget可以不需要结合 RadioButton,用它自己自带的就好了。如下:
mHost.addTab(buildTabSpec(TAB_COMMENT,"商家点评",R.drawable.tab1_selector));
mHost.addTab(buildTabSpec(TAB_DESC,"商家介绍",R.drawable.tab2_selector));
mHost.addTab(buildTabSpec(TAB_INFO,"商家信息",R.drawable.tab3_selector));
private TabHost.TabSpec buildTabSpec(String tag, String tagName,
int drawableId) {
View tagView = getTabView(drawableId, tagName);
return this.mHost.newTabSpec(tag).setIndicator(tagView)
.setContent(this);
}
private View getTabView(int drawableId, final String tabName) {
final View view = View.inflate(this, R.layout.tab_layout, null);//自定义布局
final View bgLayout = view.findViewById(R.id.bglayout);
final TextView tabView = (TextView) view.findViewById(R.id.tab);
tabView.setText(tabName);
bgLayout.setBackgroundResource(drawableId);
return view;
}
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bglayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
android:background="@drawable/near_tab1_selector"
android:orientation="vertical" >
<TextView
android:id="@+id/tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:drawablePadding="0dp"
android:textColor="@color/white"
android:padding="5dp"
android:gravity="bottom|center_horizontal"
android:textSize="11sp"
android:text="商家点评" />
</LinearLayout>
private TabHost.TabSpec buildTabSpec(String tag, String tagName,
int drawableId) {
View tagView = getTabView(drawableId, tagName);
return this.mHost.newTabSpec(tag).setIndicator(tagView)
.setContent(this);
}
private View getTabView(int drawableId, final String tabName) {
final View view = View.inflate(this, R.layout.tab_layout, null);
final View bgLayout = view.findViewById(R.id.bglayout);
final TextView tabView = (TextView) view.findViewById(R.id.tab);
tabView.setText(tabName);
bgLayout.setBackgroundResource(drawableId);
return view;
}