日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

自定義android的tab樣式 《》

系統 2867 0

android項目中碰到需要替換tabHost默認樣式的情況,需要達到下圖的效果:
自定義android的tab樣式 《》

為了做成這樣的效果,花了些時間,這里做個筆記,給有需要的朋友。

步驟一:新建一 個xml布局文件,命名main.xml

<? xml version ="1.0" encoding ="utf-8" ?>
< TabHost xmlns:android ="http://schemas.android.com/apk/res/android"
?? android:id ="@android:id/tabhost" android:layout_width ="fill_parent"
?? android:layout_height ="fill_parent" android:background ="@color/white" >
?? < 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 ="wrap_content"
?????? android:gravity ="center_horizontal|center_vertical" />
???? < FrameLayout android:id ="@android:id/tabcontent"
?????? android:layout_width ="fill_parent" android:layout_height ="fill_parent" >
???? </ FrameLayout >
?? </ LinearLayout >
</ TabHost >


步 驟二:在Activity中添加我們的tab

代碼如下

@Override
public void onCreate(Bundle savedInstanceState) {
?? super .onCreate(savedInstanceState);
??setContentView(R.layout.main);
? mTabHost = getTabHost();
??? ???
? Intent intent1 = new Intent(this,OfficialActivity.class);
? createTab("公共聊天室",intent1);
??? ???
? Intent intent2 = new Intent(this, NearByActivity.class);
? createTab("周邊",intent2);
??? ???
? Intent intent3= new Intent(this, HistoryActivity.class);
? createTab("歷史",intent3);
??? ???
? mTabHost.setCurrentTab(1);
}

?private void createTab(String text ,Intent intent){????????? mTabHost.addTab(mTabHost.newTabSpec(text).setIndicator(createTabView(text)).setContent(intent));
??? }

??? private View createTabView(String text) {
??? ??? ??? View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
??? ??? ??? TextView tv = (TextView) view.findViewById(R.id.tv_tab);
??? ??? ??? tv.setText(text);
??? ??? ??? return view;
??? }


步驟三:在createTabView方法中,我 們載入了布局文件tab_indicator.xml,改布局很簡單,就是放置一個帶背景的布局,在布局中放一個textView用于顯示tab標題,以 下為該布局文件內容

<? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
?? android:layout_height ="fill_parent" android:layout_width ="fill_parent"
?? android:orientation ="vertical" android:gravity ="center"
?? android:theme ="@android:style/Theme.NoTitleBar" android:background ="@drawable/chat_tab_selector" >
?? < TextView android:id ="@+id/tv_tab" android:layout_width ="wrap_content" android:layout_height ="wrap_content"
?? android:text ="tab1" android:textColor ="@color/white" />
</ LinearLayout >


步驟四:在上面的布局文件中我們把該布局的背景定義為 chat_tab_selector,這里同樣需要在drawable文件夾下新建chat_tab_selector.xml,內容很簡單,就是做一個 按下時的背景切換,內容如下:

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


里面做了兩張圖進行狀態切換,這兩張圖同樣放 drawable下就可以了

至此,自定義tab的步驟就完成了。如果要做成更復雜的樣式,大家可以在此基礎上進行擴展。

自定義android的tab樣式 《》


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 建始县| 格尔木市| 贵溪市| 南郑县| 丹凤县| 封开县| 巴楚县| 泰顺县| 大宁县| 锡林浩特市| 白玉县| 望谟县| 禹州市| 泰兴市| 新龙县| 武功县| 哈尔滨市| 扎兰屯市| 剑川县| 涞水县| 汝阳县| 商水县| 堆龙德庆县| 新干县| 象山县| 宿州市| 湘潭县| 海原县| 乌兰浩特市| 鄢陵县| 元阳县| 安乡县| 枞阳县| 邢台市| 抚顺市| 宜章县| 梅河口市| 成都市| 章丘市| 乌什县| 噶尔县|