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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

实现、设置-Android TabWidget-by小雨

發布時間:2023/12/10 Android 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现、设置-Android TabWidget-by小雨 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

查了好多資料,發現還是不全,干脆自己整理吧,至少保證在我的做法正確的,以免誤導讀者,也是給自己做個記錄吧!

????

首先先看一個小例子,接著講授理原

????

?

????

  • TabTest.java ??
  • ??
  • view?plaincopy?to?clipboardprint? ??
  • package?org.hualang.tab;?? ??
  • import?android.app.Activity;?? ??
  • import?android.app.TabActivity;?? ??
  • import?android.graphics.Color;?? ??
  • import?android.os.Bundle;?? ??
  • import?android.widget.TabHost;?? ??
  • import?android.widget.Toast;?? ??
  • import?android.widget.TabHost.OnTabChangeListener;?? ??
  • public?class?TabTest?extends?TabActivity?{?? ??
  • ????/**?Called?when?the?activity?is?first?created.?*/?? ??
  • ????TabHost?tabhost;?? ??
  • ????@Override?? ??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{?? ??
  • ????????super.onCreate(savedInstanceState);?? ??
  • ????????setContentView(R.layout.main);?? ??
  • ????????//獲得TabHost對象?????
  • ????????tabhost?=?getTabHost();?? ??
  • ????????//為TabHost添加簽標?????
  • ????????//新建一個newTabSpec(newTabSpec)?????
  • ????????//設置其簽標和標圖(setIndicator)?????
  • ????????//設置容內(setContent)?????
  • ????????tabhost.addTab(tabhost.newTabSpec("tab1")?? ??
  • ????????????????.setIndicator("TAB?1",getResources().getDrawable(R.drawable.img1))?? ??
  • ????????????????.setContent(R.id.text1));?? ??
  • ????????tabhost.addTab(tabhost.newTabSpec("tab2")?? ??
  • ????????????????.setIndicator("TAB?2",getResources().getDrawable(R.drawable.img2))?? ??
  • ????????????????.setContent(R.id.text2));?? ??
  • ????????tabhost.addTab(tabhost.newTabSpec("tab3")?? ??
  • ????????????????.setIndicator("TAB?3",getResources().getDrawable(R.drawable.img3))?? ??
  • ????????????????.setContent(R.id.text3));?? ??
  • ????????//設置TabHost的背景顏色?????
  • ????????//tabhost.setBackgroundColor(Color.argb(150,22,70,150));?????
  • ????????//設置TabHost的背景圖片資源?????
  • ????????tabhost.setBackgroundResource(R.drawable.bg0);?? ??
  • ????????//設置前當示顯哪個簽標?????
  • ????????tabhost.setCurrentTab(0);?? ??
  • ????????//簽標換切事件處理,setOnTabChangedListener?????
  • ????????tabhost.setOnTabChangedListener(new?OnTabChangeListener()?? ??
  • ????????{?? ??
  • ????????????public?void?onTabChanged(String?tabId)?? ??
  • ????????????{?? ??
  • ????????????????Toast?toast=Toast.makeText(getApplicationContext(),?"現在是"+tabId+"簽標",?Toast.LENGTH_SHORT);?? ??
  • ????????????????toast.show();?? ??
  • ????????????}?? ??
  • ????????});?? ??
  • ?????????? ??
  • ????}?? ??
  • }?? ??
  • ??
  • ?

    ????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">?? ??
  • ????<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"?/>?? ??
  • ????????<FrameLayout?? ??
  • ????????????android:id="@android:id/tabcontent"?? ??
  • ????????????android:layout_width="fill_parent"?? ??
  • ????????????android:layout_height="fill_parent">?? ??
  • ????????????<TextView??? ??
  • ????????????????android:id="@+id/text1"?? ??
  • ????????????????android:layout_width="fill_parent"?? ??
  • ????????????????android:layout_height="fill_parent"??? ??
  • ????????????????android:text="選項卡1"?/>?? ??
  • ????????????<TextView??? ??
  • ????????????????android:id="@+id/text2"?? ??
  • ????????????????android:layout_width="fill_parent"?? ??
  • ????????????????android:layout_height="fill_parent"??? ??
  • ????????????????android:text="選項卡2"?/>?? ??
  • ????????????<TextView??? ??
  • ????????????????android:id="@+id/text3"?? ??
  • ????????????????android:layout_width="fill_parent"?? ??
  • ????????????????android:layout_height="fill_parent"??? ??
  • ????????????????android:text="選項卡3"?/>?? ??
  • ????????</FrameLayout>?? ??
  • ????</LinearLayout>?? ??
  • </TabHost>? ??
  • ????


    ????

    Android TabWidget的實現可以分為二種,一種是應用準標TabActivity實現,另外一種可以自定義方法實現,種這方法實現起來對相較比復雜,但對于要實現較比多元化的view是很好的,這里我們簡略看下源碼

    ????

    一、通用做法

    ????

    繼承TabActivity,實現自己的TabActivity

    ????

    ?

    ????

    [java]?view plaincopy
  • import?android.app.Activity;??
  • import?android.app.TabActivity;??
  • import?android.content.Intent;??
  • import?android.os.Bundle;??
  • import?android.widget.TabHost;??
  • import?android.widget.TabHost.OnTabChangeListener;??
  • public?class?TabWidgetDemo2?extends?TabActivity?implements?OnTabChangeListener?{??
  • ?????private?TabHost?mTabHost;??
  • ???????
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onCreate(savedInstanceState);??
  • ??????????
  • ????????setContentView(R.layout.tabwidgetdemo2);????
  • ????????mTabHost?=?getTabHost();??
  • ????????mTabHost.setOnTabChangedListener(this);??
  • ????????setupTab1();??
  • ????????setupTab2();??
  • ????????mTabHost.setCurrentTab(1);??
  • ????}??
  • ????private?void?setupTab2()?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????Intent?intent?=?new?Intent();??
  • ????????intent.setAction(Intent.ACTION_MAIN);??
  • ????????intent.setClass(this,?TabWidget2.class);??
  • ????????mTabHost.addTab(mTabHost.newTabSpec("TabWidget2")??
  • ????????????????.setIndicator("TabWidget2",getResources().getDrawable(R.drawable.icon))??
  • ????????????????.setContent(intent));??
  • ????}??
  • ????private?void?setupTab1()?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????Intent?intent?=?new?Intent();??
  • ????????intent.setAction(Intent.ACTION_MAIN);??
  • ????????intent.setClass(this,?TabWidget1.class);??
  • ????????mTabHost.addTab(mTabHost.newTabSpec("TabWidget1")??
  • ????????????????.setIndicator("TabWidget1",getResources().getDrawable(R.drawable.icon))??
  • ????????????????.setContent(intent));??
  • ????}??
  • ????public?void?onTabChanged(String?tabId)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????Activity?activity?=?getLocalActivityManager().getActivity(tabId);??
  • ????????if?(activity?!=?null)?{??
  • ????????????activity.onWindowFocusChanged(true);??
  • ????????}??
  • ????}??
  • ???????
  • ???????
  • }??
  • ????

    二個tab對應的Activity,先看TabWidget1,這個類在第二種實現中還會用到,因此我們可以看到對Action的判斷。

    ????

    ?

    ????

    [java]?view plaincopy
  • import?android.app.Activity;??
  • import?android.content.Intent;??
  • import?android.os.Bundle;??
  • import?com.android.exampledemo.R;??
  • import?com.android.exampledemo.util.DemoUtils;??
  • public?class?TabWidget1?extends?Activity?{??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onCreate(savedInstanceState);??
  • ??????????
  • ????????Intent?intent?=?this.getIntent();??
  • ????????if?(intent.getAction().equals(Intent.ACTION_MAIN)){??
  • ????????????setContentView(R.layout.tabwidgetdemo2_1);??
  • ????????}??
  • ????????else?{??
  • ????????????setContentView(R.layout.tabwidget_1);??
  • ????????????DemoUtils.updateButtonBar((Activity)this,R.id.contactstab);??
  • ????????}??
  • ????}??
  • }??
  • ????

    ?

    ????

    再看一下TabWidget2,這個Activity我們在第二種實現方法中也會用到。

    ????

    ?

    ????

    [java]?view plaincopy
  • import?com.android.exampledemo.R;??
  • import?com.android.exampledemo.util.DemoUtils;??
  • import?android.app.Activity;??
  • import?android.content.Intent;??
  • import?android.os.Bundle;??
  • public?class?TabWidget2?extends?Activity?{??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onCreate(savedInstanceState);??
  • ??????????
  • ????????Intent?intent?=?this.getIntent();??
  • ??????????
  • ????????if?(intent.getAction().equals(Intent.ACTION_MAIN)){??
  • ????????????setContentView(R.layout.tabwidgetdemo2_1);??
  • ????????}??
  • ????????else?{??
  • ????????????setContentView(R.layout.tabwidget_2);??
  • ????????????DemoUtils.updateButtonBar((Activity)this,R.id.groupstab);??
  • ????????}??
  • ????}??
  • }??
  • ????

    ?

    ????

    最后就是各個Activity對應的layout

    ????

    1.tabwidgetdemo2.xml

    ????

    ?

    ????

    [xhtml]?view plaincopy
  • <?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">??
  • ??<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="68dip"??
  • ????????android:paddingLeft="1dip"??
  • ????????android:paddingRight="1dip"??
  • ????????android:paddingTop="4dip"??
  • ????????/>??
  • ????<FrameLayout?android:id="@android:id/tabcontent"??
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="0dip"??
  • ????????android:layout_weight="1"??
  • ????????/>??
  • ????</LinearLayout>???
  • </TabHost>??
  • ????

    2.二個sub tab對應的layout

    ????

    ?

    ????

    [xhtml]?view plaincopy
  • Layout1??
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout??
  • ??xmlns:android="http://schemas.android.com/apk/res/android"??
  • ??android:layout_width="fill_parent"??
  • ??android:layout_height="fill_parent"??
  • ??android:background="#FFF">??
  • ??<TextView?android:id="@+id/textview"???
  • ????android:layout_width="wrap_content"???
  • ????android:layout_height="wrap_content"??
  • ????android:text="Tab?Widget?first">??
  • ???</TextView>??
  • </LinearLayout>??
  • Layout2??
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout??
  • ??xmlns:android="http://schemas.android.com/apk/res/android"??
  • ??android:layout_width="fill_parent"??
  • ??android:layout_height="fill_parent"??
  • ??android:background="#FFF">??
  • ??<TextView?android:id="@+id/textview"???
  • ????android:layout_width="wrap_content"???
  • ????android:layout_height="wrap_content"??
  • ????android:text="Tab?Widget?second">??
  • ???</TextView>??
  • </LinearLayout>??
  • ????

    ?

    ????

    ?

    ????

    方法2:

    ????

    先創立一個Activity (TabWidgetDemo)

    ????

    ?

    ????

    [c-sharp]?view plaincopy
  • 1.TabWidgetDemo.java??
  • import?com.android.exampledemo.R;??
  • import?com.android.exampledemo.util.DemoUtils;??
  • import?android.app.Activity;??
  • import?android.content.Context;??
  • import?android.content.SharedPreferences;??
  • import?android.os.Bundle;??
  • //not?use?tabhost?to?organized???
  • public?class?TabWidgetDemo?extends?Activity?{??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onCreate(savedInstanceState);??
  • ????????//int?activeTab?=?DemoUtils.getIntPref(this,?"activetab",?R.id.artisttab);??
  • ????????SharedPreferences?prefs?=??
  • ????????????getSharedPreferences(getPackageName(),?Context.MODE_PRIVATE);??
  • ????????int?activeTab?=?prefs.getInt("activetab",?R.id.contactstab);??
  • ????????if?(activeTab?!=?R.id.contactstab??
  • ????????????????&&?activeTab?!=?R.id.groupstab)?{??
  • ????????????activeTab?=?R.id.contactstab;??
  • ????????}??
  • ????????DemoUtils.activateTab(this,?activeTab);??
  • ????}??
  • }??
  • 2.DemoUtils??
  • import?android.app.Activity;??
  • import?android.content.Intent;??
  • import?android.net.Uri;??
  • import?android.view.View;??
  • import?android.widget.TabWidget;??
  • import?com.android.exampledemo.R;??
  • public?class?DemoUtils?{??
  • ????static?int?sActiveTabIndex?=?-1;??
  • ??????
  • ????public?static?void?activateTab(Activity?a,int?active_id){??
  • ????????Intent?intent?=?new?Intent(Intent.ACTION_PICK);??
  • ????????switch?(active_id)?{??
  • ????????case?R.id.contactstab:??
  • ????????????intent.setDataAndType(Uri.EMPTY,?"vnd.android.cursor.dir/tb_contacts");??
  • ????????????break;??
  • ????????case?R.id.groupstab:??
  • ????????????intent.setDataAndType(Uri.EMPTY,?"vnd.android.cursor.dir/tb_groups");??
  • ????????????break;??
  • ????????default:??
  • ????????????return;??
  • ????????}??
  • ????????intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);??
  • ????????a.startActivity(intent);??
  • ????????a.finish();??
  • ????????a.overridePendingTransition(0,0);??
  • ????}??
  • ??????
  • ??????
  • ????public?static?void?updateButtonBar(Activity?a,?int?highlight)?{??
  • ????????final?TabWidget?ll?=?(TabWidget)?a.findViewById(R.id.buttonbar);??
  • ??????????
  • ?????????for?(int?i?=?ll.getChildCount()?-?1;?i?>=?0;?i--)?{??
  • ?????????????View?v?=?ll.getChildAt(i);??
  • ?????????????boolean?isActive?=?(v.getId()?==?highlight);??
  • ?????????????if?(isActive)?{??
  • ????????????????????ll.setCurrentTab(i);??
  • ????????????????????sActiveTabIndex?=?i;??
  • ?????????????}??
  • ???????????????
  • ?????????????v.setTag(i);??
  • ?????????????v.setOnClickListener(new?View.OnClickListener()?{??
  • ????????????????????public?void?onClick(View?v)?{??
  • ????????????????????????int?id?=?v.getId();??
  • ????????????????????????if?(id?==?ll.getChildAt(sActiveTabIndex).getId())?{??
  • ????????????????????????????return;??
  • ????????????????????????}??
  • ????????????????????????activateTab((Activity)ll.getContext(),id?);??
  • ????????????????????????ll.setCurrentTab((Integer)?v.getTag());??
  • ????????????????????}});??
  • ?????????}??
  • ????}??
  • }??
  • ?????

    ????

    ?

    ????

    二個Tab sub activity前一方法中經已給出,這里我們只需要看一下layout的實現就能夠了

    ????

    1>buttonbar.xml

    ????

    ?

    ????

    [xhtml]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <TabWidget?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:id="@+id/buttonbar"??
  • ????android:layout_width="match_parent"??
  • ????android:layout_height="wrap_content"?>??
  • ????<TextView??
  • ????????android:id="@+id/contactstab"??
  • ????????android:focusable="true"??
  • ????????android:drawableTop="@drawable/icon"??
  • ????????android:background="@drawable/buttonbarbackground"??
  • ????????android:text="Contacts"??
  • ????????android:textColor="@color/tab_indicator_text"??
  • ????????android:textAppearance="?android:attr/textAppearanceSmall"??
  • ????????android:paddingTop="7dip"??
  • ????????android:paddingBottom="2dip"??
  • ????????android:gravity="center"??
  • ????????android:layout_weight="1"??
  • ????????android:layout_marginLeft="-3dip"??
  • ????????android:layout_marginRight="-3dip"??
  • ????????android:layout_width="match_parent"??
  • ????????android:layout_height="84dip"??
  • ????????android:singleLine="true"??
  • ????????android:ellipsize="marquee"?/>??
  • ????<TextView??
  • ????????android:id="@+id/groupstab"??
  • ????????android:focusable="true"??
  • ????????android:drawableTop="@drawable/icon"??
  • ????????android:background="@drawable/buttonbarbackground"??
  • ????????android:text="Group"??
  • ????????android:textColor="@color/tab_indicator_text"??
  • ????????android:textAppearance="?android:attr/textAppearanceSmall"??
  • ????????android:paddingTop="7dip"??
  • ????????android:paddingBottom="2dip"??
  • ????????android:gravity="center"??
  • ????????android:layout_weight="1"??
  • ????????android:layout_marginLeft="-3dip"??
  • ????????android:layout_marginRight="-3dip"??
  • ????????android:layout_width="match_parent"??
  • ????????android:layout_height="84dip"??
  • ????????android:singleLine="true"??
  • ????????android:ellipsize="marquee"?/>??
  • </TabWidget>??
  • ????

    2>tabwidget_1.xml

    ????

    ?

    ????

    [xhtml]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout??
  • ??xmlns:android="http://schemas.android.com/apk/res/android"??
  • ??android:layout_width="fill_parent"??
  • ??android:layout_height="fill_parent">??
  • ????
  • ??<include?layout="@layout/battonbar"?/>??
  • ????
  • ??<ExpandableListView?android:id="@+id/android:list"??
  • ??????????????????android:layout_width="fill_parent"???
  • ??????????????????android:layout_height="wrap_content"??
  • ??????????????????android:footerDividersEnabled="true"??
  • ??????????????????android:fadeScrollbars="true"??
  • ??????????????????android:drawSelectorOnTop="true">??
  • ??</ExpandableListView>??
  • ????
  • </LinearLayout>??
  • ????

    3> tabwidget_2.xml

    ????

    ?

    ????

    [xhtml]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout??
  • ??xmlns:android="http://schemas.android.com/apk/res/android"??
  • ??android:layout_width="fill_parent"??
  • ??android:layout_height="fill_parent">??
  • ????
  • ??<include?layout="@layout/battonbar"?/>??
  • ????
  • </LinearLayout>??
  • ????

    ?

    文章結束給大家分享下程序員的一些笑話語錄: 真正的程序員喜歡兼賣爆米花,他們利用CPU散發出的熱量做爆米花,可以根據米花爆裂的速度聽出正在運行什么程序。

    轉載于:https://www.cnblogs.com/jiangu66/archive/2013/04/14/3020375.html

    總結

    以上是生活随笔為你收集整理的实现、设置-Android TabWidget-by小雨的全部內容,希望文章能夠幫你解決所遇到的問題。

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