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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

利用TabWidget实现底部菜单

發布時間:2024/7/23 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用TabWidget实现底部菜单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

TabWidget類似于通話記錄的界面,通過切換多個標簽從而顯示出多個不同內容,能夠展示內容豐富的頁面信息,而且彼此之間不會干擾,有利于展示。下面,通過一個例子來學習用法

首先用一個類來繼承TabActivity

在開發之前,我們要首先了解,TabHost是整個Tab的容器,包括兩部分,TabWidget和FrameLayout。TabWidget就是每個tab的標簽,FrameLayout則是tab內容。接著我們開始初始化main.xml。
首先聲明TabHost,包含TabWidget,FrameLayout元素。

<TabHost android:id="@android:id/tabhost" //聲明控件IDandroid:layout_width="fill_parent" //控件寬度與父控件一致android:layout_height="fill_parent"> //控件高度與父控件一致 聲明TabWidget,tab標簽頁<TabWidget android:layout_width="fill_parent" //控件寬度與父控件一致android:layout_height="wrap_content" //控件高度與自身適應android:id="@android:id/tabs"> //聲明控件ID 聲明FrameLayout,tab頁里的內容信息<FrameLayout android:layout_width="fill_parent" //控件寬度與父控件一致android:layout_height="wrap_content" //控件高度與自身適應android:id="@android:id/tabcontent"> //聲明控件ID

注意下:
如果我們使用extends TabAcitivty,如同ListActivity,TabHost必須設置為@android:id/tabhost
TabWidget必須設置android:id為@android:id/tabs
FrameLayout需要設置android:id為@android:id/tabcontent

布局文件

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@android:id/tabhost" ><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent"android:layout_height="0.0dip"android:layout_weight="1.0"/><TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent"android:layout_height="wrap_content"android:visibility="gone"/><RadioGroup android:id="@+id/tab_items"android:gravity="center_vertical"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_gravity="bottom"android:background="@drawable/tab_bg"><RadioButton android:id="@+id/tab_item_home"android:checked="true" style="@style/main_tab_bottom"android:background="@drawable/item_home_bg" /><RadioButton android:id="@+id/tab_item_nearby" style="@style/main_tab_bottom"android:background="@drawable/item_near_bg"/><RadioButton android:id="@+id/tab_item_sort" style="@style/main_tab_bottom"android:background="@drawable/item_sort_bg" /><RadioButton android:id="@+id/tab_item_mine" style="@style/main_tab_bottom"android:background="@drawable/item_mine_bg"/> <RadioButton android:id="@+id/tab_item_more" style="@style/main_tab_bottom" android:background="@drawable/item_more_bg" /></RadioGroup></LinearLayout></TabHost>

其中有些控件的圖片點擊與正常情況下是不同的,如item_home_bg.xml文件

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:state_checked="true" android:drawable="@drawable/but_index_r_v2" /><item android:drawable="@drawable/but_index_v2"/> </selector>

style文件在values文件夾下的styles.xml文件中定義

<?xml version="1.0" encoding="utf-8"?> <resources><style name="main_tab_bottom"><item name="android:gravity">center_horizontal</item><item name="android:layout_width">fill_parent</item><item name="android:layout_height">wrap_content</item><item name="android:button">@null</item><item name="android:layout_weight">1.0</item></style></resources>

函數實現

public class MyTab extends TabActivity{private final static String TAG = "TabShow";private TabHost mHost;private RadioGroup tabItems;private RadioButton mineBut;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.tablay);initResourceRefs();initSettings();}private void initSettings() {// TODO Auto-generated method stubtabItems.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch(checkedId){case R.id.tab_item_home :mHost.setCurrentTabByTag("HOME");break;case R.id.tab_item_nearby :mHost.setCurrentTabByTag("NEAR");break;case R.id.tab_item_sort :mHost.setCurrentTabByTag("SORT");break; case R.id.tab_item_more :mHost.setCurrentTabByTag("MORE");break;}}});}private void initResourceRefs() {// TODO Auto-generated method stubmHost = getTabHost();mHost.addTab(mHost.newTabSpec("HOME").setIndicator("HOME").setContent(new Intent(this , HomeActivity.class)));mHost.addTab(mHost.newTabSpec("NEAR").setIndicator("NEAR").setContent(new Intent(this , NearByActivity.class)));mHost.addTab(mHost.newTabSpec("SORT").setIndicator("SORT").setContent(new Intent(this , SortActivity.class)));mHost.addTab(mHost.newTabSpec("My").setIndicator("My").setContent(new Intent(this , MyActivity.class)));mHost.addTab(mHost.newTabSpec("MORE").setIndicator("MORE").setContent(new Intent(this , MoreActivity.class)));tabItems = (RadioGroup)findViewById(R.id.tab_items);mineBut = (RadioButton)findViewById(R.id.tab_item_mine);}}

效果如下

總結

以上是生活随笔為你收集整理的利用TabWidget实现底部菜单的全部內容,希望文章能夠幫你解決所遇到的問題。

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