TabHost两种实现方式
生活随笔
收集整理的這篇文章主要介紹了
TabHost两种实现方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一種:繼承TabActivity,從TabActivity中用getTabHost()方法獲取TabHost。只要定義具體Tab內容布局就行了.?
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="所有通話記錄"></TextView><TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已接來電"></TextView><TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未接來電"></TextView></FrameLayout>
package com.example.testtabhost;import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.Toast; import android.widget.TabHost.OnTabChangeListener;public class MainActivity extends TabActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TabHost th = getTabHost();//聲明TabHost,然后用LayoutInflater過濾出布局來,給TabHost加上含有Tab頁面的FrameLayout//from(this)從這個TabActivity獲取LayoutInflater //R.layout.main 存放Tab布局//通過TabHost獲得存放Tab標簽頁內容的FrameLayout //是否將inflate 拴系到根布局元素上LayoutInflater.from(this).inflate(R.layout.activity_main, th.getTabContentView(), true); //通過TabHost獲得存放Tab標簽頁內容的FrameLayout,//newTabSpecd的作用是獲取一個新的 TabHost.TabSpec,并關聯到當前 TabHost//setIndicator的作用是指定標簽和圖標作為選項卡的指示符.//setContent的作用是指定用于顯示選項卡內容的視圖 ID.th.addTab(th.newTabSpec("all").setIndicator("所有通話記錄", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView01));th.addTab(th.newTabSpec("ok").setIndicator("已接來電",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView02));th.addTab(th.newTabSpec("cancel").setIndicator("未接來電",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.TextView03));//setOnTabChangeListener的作業是注冊一個回調函數,當任何一個選項卡的選中狀態發生改變時調用. th.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_LONG).show();}}); } }
第二種:不用繼承TabActivity,在布局文件中定義TabHost即可,但是TabWidget的id必須是?
@android:id/tabs,FrameLayout的id必須是@android:id/tabcontent。TabHost的id可以自定義.?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hometabs"android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabHost android:id="@+id/tabhost"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TabWidget android:id="@android:id/tabs" android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"></TabWidget><FrameLayout android:id="@android:id/tabcontent"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView android:id="@+id/view1"android:layout_width="fill_parent"android:layout_height="fill_parent"/><TextView android:id="@+id/view2"android:layout_width="fill_parent"android:layout_height="fill_parent"/><TextView android:id="@+id/view3"android:layout_width="fill_parent"android:layout_height="fill_parent"/></FrameLayout></LinearLayout></TabHost> </LinearLayout>
package com.example.testtabhost2;import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TabHost; import android.widget.TabWidget;public class MainActivity extends Activity {private static final String TAG = "MainActivity";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TabHost tabHost = (TabHost) findViewById(R.id.tabhost);tabHost.setup();TabWidget tabWidget = tabHost.getTabWidget();tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.view3));tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.view2));final int tabs = tabWidget.getChildCount();Log.i(TAG, "***tabWidget.getChildCount() : " + tabs);final int tabWidth = 90;final int tabHeight = 45;for (int i = 0; i < tabs; i++) {/* final View view = tabWidget.getChildAt(i);view.getLayoutParams().width = tabWidth;view.getLayoutParams().height = tabHeight;final TextView tv = (TextView) view.findViewById(android.R.id.title);tv.setTextColor(this.getResources().getColorStateList(android.R.color.black));MarginLayoutParams tvMLP = (MarginLayoutParams)tv.getLayoutParams();tvMLP.bottomMargin = 8;*/}}}
轉載于:https://www.cnblogs.com/lanzhi/p/6469829.html
總結
以上是生活随笔為你收集整理的TabHost两种实现方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux集群服务知识点总结及通过案例介
- 下一篇: Jafka源码粗略解读之二--关于JMX