新浪微博布局学习——妙用TabHost

转载至:新浪微博布局学习——妙用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;
	}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值