The important thing in life is to have a great aim , and the determination

          常用鏈接

          統(tǒng)計(jì)

          IT技術(shù)鏈接

          保險(xiǎn)相關(guān)

          友情鏈接

          基金知識(shí)

          生活相關(guān)

          最新評(píng)論

          修改TabHost默認(rèn)樣式(文字居中)

          修改TabHost默認(rèn)樣式

          TabHost是Android提供的一個(gè)容器組件,利用它可以輕松地實(shí)現(xiàn)TAB界面,如下圖所示:

          但很多時(shí)候,默認(rèn)的TAB樣式并不符合軟件的整體風(fēng)格,這時(shí)候該怎么辦呢?其實(shí),我們可以編寫XML對(duì)其樣式進(jìn)行修改。下面修改后的效果圖:

          1. TabHost布局文件 main.xml

          	<TabHost
          	    android:id="@+id/tabhost"  
          	    android:layout_width="fill_parent"
          	    android:layout_height="fill_parent">
          <LinearLayout  
          	        android:orientation="vertical"
          	        android:layout_width="fill_parent"  
          	        android:layout_height="fill_parent">
          <TabWidget  
          	            android:id="@android:id/tabs"
          	            android:layout_width="fill_parent" 
          	            android:layout_height="30dip"
          	            android:background="#a0a0a0"
          	            android:layout_weight="0" />
          <FrameLayout  
          	            android:id="@android:id/tabcontent"  
          	            android:layout_width="fill_parent"  
          	            android:layout_height="fill_parent"
          	            android:layout_weight="1">
          <ListView 
          				    android:id="@+id/user_list"
          				    android:layout_width="fill_parent"
          				    android:layout_height="fill_parent" 
          				    android:divider="@drawable/divider_line"
          				    android:cacheColorHint="#00000000" />
          <ListView 
          				    android:id="@+id/article_list"
          				    android:layout_width="fill_parent"
          				    android:layout_height="fill_parent" 
          				    android:divider="@drawable/divider_line"
          				    android:cacheColorHint="#00000000" />
          <ListView 
          				    android:id="@+id/feed_list"
          				    android:layout_width="fill_parent"
          				    android:layout_height="fill_parent" 
          				    android:divider="@drawable/divider_line"
          				    android:cacheColorHint="#00000000" />
          <ListView 
          				    android:id="@+id/book_list"
          				    android:layout_width="fill_parent"
          				    android:layout_height="fill_parent" 
          				    android:divider="@drawable/divider_line"
          				    android:cacheColorHint="#00000000" />
          </FrameLayout>
          </LinearLayout>
          </TabHost>


          FrameLayout里有四個(gè)ListView 分別對(duì)應(yīng)用戶、文章、頻道、圖書。
          TabWidget和FrameLayout的ID不能自己定義修改。




          2. Activity后臺(tái)代碼
          RelativeLayout articleTab = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.minitab, null);
                      TextView articleTabLabel = (TextView) articleTab.findViewById(R.id.tab_label);
                      articleTabLabel.setText("文章");
                       
                      RelativeLayout feedTab = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.minitab, null);
                      TextView feedTabLabel = (TextView) feedTab.findViewById(R.id.tab_label);
                      feedTabLabel.setText("頻道");
                       
                      RelativeLayout bookTab = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.minitab, null);
                      TextView bookTabLabel = (TextView) bookTab.findViewById(R.id.tab_label);
                      bookTabLabel.setText("圖書");
                       
                      TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
                      tabHost.setup();
                       
                      tabHost.addTab(tabHost.newTabSpec("user").setIndicator(userTab).setContent(R.id.user_list));
                      tabHost.addTab(tabHost.newTabSpec("article").setIndicator(articleTab).setContent(R.id.article_list));
                      tabHost.addTab(tabHost.newTabSpec("feed").setIndicator(feedTab).setContent(R.id.feed_list));
                      tabHost.addTab(tabHost.newTabSpec("book").setIndicator(bookTab).setContent(R.id.book_list));


          TabHost創(chuàng)建出來以后,必須先setup一下,tabHost.setup();

          setIndicator方法設(shè)置的View其實(shí)就對(duì)應(yīng)了TAB中的一個(gè)個(gè)選項(xiàng)卡,它們都是通過一個(gè)叫minitab的布局文件inflate出來的
          3. 選項(xiàng)卡布局文件minitab.xml

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:paddingLeft="5dip"
              android:paddingRight="5dip">
           
          <TextView android:id="@+id/tab_label"  
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:gravity="center"
                  android:textColor="#000000"
                  android:textStyle="bold"
                  android:background="@drawable/minitab" />
          </RelativeLayout>

          drawable/minitab是一個(gè)selector,指定了Tab選項(xiàng)卡的背景顏色。

          4. selector文件 minitab.xml

          <?xml version="1.0" encoding="utf-8"?>
          <selector
          	xmlns:android="http://schemas.android.com/apk/res/android"
          	>
          <item
          		android:state_selected="false"
          		android:drawable="@drawable/minitab_unselected"
          		>
          </item>
          <item
          		android:state_selected="true"
          		android:drawable="@drawable/minitab_default"
          		>
          </item>
          </selector>


          minitab_unselected是一淺藍(lán)色背景圖片
          minitab_default是一白色背景圖片




          posted on 2011-02-16 11:28 鴻雁 閱讀(5992) 評(píng)論(0)  編輯  收藏 所屬分類: IT技術(shù)相關(guān)

          主站蜘蛛池模板: 宽城| 图木舒克市| 德钦县| 漯河市| 灵川县| 调兵山市| 新宾| 屯门区| 延吉市| 汕头市| 东乡族自治县| 从化市| 柳江县| 大悟县| 濮阳县| 连云港市| 溧阳市| 颍上县| 洪泽县| 大名县| 兰州市| 开平市| 公主岭市| 察隅县| 晋江市| 德格县| 巢湖市| 贵州省| 太和县| 长沙县| 石棉县| 科技| 汝城县| 嘉黎县| 宝坻区| 突泉县| 鹤壁市| 彭山县| 眉山市| 柘城县| 泰顺县|