日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义TabHost,TabWidget样式 .

發布時間:2025/4/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义TabHost,TabWidget样式 . 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大家好,今天我為大家分享TabHost中怎樣修改TabWidget樣式。在很多界面美觀的應用中很多都用到了TabHost,但他們要比系統默認的要漂亮得多。先看幾張圖:

?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?京東商城底部菜單欄

? ? ? ? ? ? ? ? ? ? ? ? ? ? 新浪微博底部菜單欄

? ?好了,看到這些漂亮的菜單欄是不是很驚訝,你可能會說用Button就可以實現啊 ,可是用Button的話控制顯示的內容很麻煩,不如用TabHost控制效率更高。很想知道用TabHost是怎么實現的吧,下面就來研究如何實現這種漂亮的TabHost。先看一下效果圖:

?

界面比較簡單,要想做得漂亮換幾張圖片就可以了。

? 第一步:先在布局(這里用了main.xml創建時自動生成的)里面放上TabHost ,只要將TabHost控件托至屏幕中就可:

[html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • ????<TabHost?android:id="@+id/tabhost"???
  • ??????android:layout_width="fill_parent"???
  • ??????android:layout_height="fill_parent"???
  • ??????xmlns:android="http://schemas.android.com/apk/res/android">??
  • ????????<LinearLayout?android:layout_width="fill_parent"???
  • ??????????android:id="@+id/linearLayout1"???
  • ??????????android:layout_height="fill_parent"???
  • ??????????android:orientation="vertical">??
  • ????????????<TabWidget?android:layout_width="fill_parent"???
  • ??????????????android:layout_height="wrap_content"???
  • ??????????????android:id="@android:id/tabs"></TabWidget>??
  • ????????????<FrameLayout?android:layout_width="fill_parent"?android:layout_height="fill_parent"?android:id="@android:id/tabcontent">??
  • ????????????????<LinearLayout?android:layout_width="fill_parent"?android:layout_height="fill_parent"?android:id="@+id/tab1"></LinearLayout>??
  • ????????????????<LinearLayout?android:layout_width="fill_parent"?android:layout_height="fill_parent"?android:id="@+id/tab2"></LinearLayout>??
  • ????????????????<LinearLayout?android:layout_width="fill_parent"?android:layout_height="fill_parent"?android:id="@+id/tab3"></LinearLayout>??
  • ????????????</FrameLayout>??
  • ????????</LinearLayout>??
  • ????</TabHost>??

  • 這里我們已經把LinearLayout和TextView去掉了,并將“xmlns:android="……" ”添加大TabHost里了,這里要注意我們將TabHost的id定義為自己定義的id比不用android規定的id="@android:id/tabhost"。

    ?

    第二步:創建顯示此TabWidget的布局tabmini.xml:

    [html]?view plaincopy
  • <?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="40dp"??
  • ????android:paddingLeft="5dip"??
  • ????android:paddingRight="5dip"??
  • ????android:background="@drawable/head_bg">????
  • ??????
  • ????<TextView?android:id="@+id/tab_label"????
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:layout_centerInParent="true"??
  • ????????android:gravity="center"??
  • ????????android:textColor="#000000"??
  • ????????android:textStyle="bold"??
  • ????????android:background="@drawable/tabmini"/>???
  • </RelativeLayout>??

  • 第三步:創建一個selector在drawable里面 命名tabmini.xml,用來點擊TabHost的一個tab時TextView的變化:

    [html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <selector??
  • ??xmlns:android="http://schemas.android.com/apk/res/android">??
  • ????<item?android:state_selected="true"??
  • ????????android:drawable="@drawable/add_managebg_down"/>??
  • ????<item?android:state_selected="false"??
  • ????????android:drawable="@drawable/add_managebg"/>??
  • </selector>??

  • 第四步:在Activity里實現TabHost:

    [java]?view plaincopy
  • package?cn.li.tabstyle;??
  • ??
  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.widget.TabHost;??
  • import?android.widget.TextView;??
  • ??
  • public?class?TabHostStyleActivity?extends?Activity?{??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ??????????
  • ????????View?niTab?=?(View)?LayoutInflater.from(this).inflate(R.layout.tabmini,?null);??
  • ????????TextView?text0?=?(TextView)?niTab.findViewById(R.id.tab_label);??
  • ????????text0.setText("ni");??
  • ??????????
  • ????????View?woTab?=?(View)?LayoutInflater.from(this).inflate(R.layout.tabmini,?null);??
  • ????????TextView?text1?=?(TextView)?woTab.findViewById(R.id.tab_label);??
  • ????????text1.setText("wo");??
  • ??????????
  • ????????View?taTab?=?(View)?LayoutInflater.from(this).inflate(R.layout.tabmini,?null);??
  • ????????TextView?text2?=?(TextView)?taTab.findViewById(R.id.tab_label);??
  • ????????text2.setText("ta");??
  • ??????????
  • ????????View?weTab?=?(View)?LayoutInflater.from(this).inflate(R.layout.tabmini,?null);??
  • ????????TextView?text3?=?(TextView)?weTab.findViewById(R.id.tab_label);??
  • ????????text3.setText("we");??
  • ??????????
  • ????????TabHost?tabHost?=?(TabHost)findViewById(R.id.tabhost);??
  • ????????tabHost.setup();???//Call?setup()?before?adding?tabs?if?loading?TabHost?using?findViewById().???
  • ??????????
  • ????????tabHost.addTab(tabHost.newTabSpec("nitab").setIndicator(niTab).setContent(R.id.tab1));??
  • ????????tabHost.addTab(tabHost.newTabSpec("wotab").setIndicator(woTab).setContent(R.id.tab2));??
  • ????????tabHost.addTab(tabHost.newTabSpec("tatab").setIndicator(taTab).setContent(R.id.tab3));??
  • ????????tabHost.addTab(tabHost.newTabSpec("wetab").setIndicator(weTab).setContent(R.id.tab4));??
  • ????}??
  • }??

  • 這里我們用findViewById創建了TabHost,這樣的話我們就需要在添加tab時調用TabHost的setup()方法;這里我們添加內容時添加的是布局,我們完全可以換成自己創建的Activity。

    好了,讓我們來看看運行效果吧:

    ?



    好了,我們自定義的TabHost算是結束了。不過看到Activity里的代碼很多都是重復的我們可以這樣把他們簡化:

    [java]?view plaincopy
  • package?cn.li.tabstyle;??
  • ??
  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.widget.TabHost;??
  • import?android.widget.TextView;??
  • ??
  • public?class?TabHostStyleActivity?extends?Activity?{??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????String[]?title?=?new?String[]{"ni","wo","ta","we"};??
  • ????View?userTab,articeTab,feedTab,weTab;??
  • ????View[]?tabs?=?new?View[]{userTab,articeTab,feedTab,weTab};??
  • ????int[]?tabIds?=?new?int[]{R.id.tab1,R.id.tab2,R.id.tab3,R.id.tab4};??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ??????????
  • ????????TabHost?tabHost?=?(TabHost)findViewById(R.id.tabhost);??
  • ????????tabHost.setup();???//Call?setup()?before?adding?tabs?if?loading?TabHost?using?findViewById().???
  • ??????????
  • ????????for(int?i=0;i<tabs.length;i++){??
  • ????????????tabs[i]?=?(View)?LayoutInflater.from(this).inflate(R.layout.tabmini,?null);??
  • ????????????TextView?text?=?(TextView)?tabs[i].findViewById(R.id.tab_label);??
  • ????????????text.setText(title[i]);??????????????
  • ????????????tabHost.addTab(tabHost.newTabSpec(title[i]).setIndicator(tabs[i]).setContent(tabIds[i]));??
  • ????????}??
  • ????}??
  • }??

  • ?

    轉載于:https://www.cnblogs.com/firecode/archive/2012/12/13/2815960.html

    總結

    以上是生活随笔為你收集整理的自定义TabHost,TabWidget样式 .的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。