android tabhost --android UI 学习
最近由于项目原因,需要使用tabhost,写一下自己的使用方法
实现TabHost有三种方式:继承自TabActivity,ActivityGroup和自定义的Activity
1.使用TabAcitvity
TabActivity他自己包含一个Tabhost,我们通过getTabhost(),也不需要调用setContentView()设置layout。如果设置一定要按照android SDK的规定进行设置。SDK规定的是:TabHost,TabWidget,FrameLayout的id必须为@android:id/tabhost,@android:id/tabs
,@android:id/tabcontent;
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_height="match_parent"
android:layout_width="fill_parent" android:background="@drawable/inde_bg">
<LinearLayout android:orientation="vertical" android:id="@+id/layout1"
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">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</TabHost>
否则会报什么需要设置相应的id。
实现代码如下(需要的图片包括在zip文件中):
public class IndexActicity extends TabActivity {private int index_tab = 0;private TabWidget tabWidget;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);Constant.WIDTH = dm.widthPixels;Constant.HEIGHT= dm.heightPixels;TabHost t =getTabHost();t.setBackgroundDrawable(getResources().getDrawable(R.drawable.inde_bg));t.setPadding(0, 0, 0, 0);tabWidget = t.getTabWidget();LayoutInflater fi = LayoutInflater.from(IndexActicity.this);View view = fi.inflate(R.layout.tab_layout, null);LinearLayout ll = (LinearLayout) view.findViewById(R.id.tablayout);View tab1 = view.findViewById(R.id.tab1);View tab2 = view.findViewById(R.id.tab2);View tab3 = view.findViewById(R.id.tab3);View tab4 = view.findViewById(R.id.tab4);ll.removeAllViews();t.addTab(t.newTabSpec("1").setIndicator(tab1).setContent(new Intent(IndexActicity.this,TabActivity1.class)));t.addTab(t.newTabSpec("2").setIndicator(tab2).setContent(new Intent(IndexActicity.this,TabActivity2.class)));t.addTab(t.newTabSpec("3").setIndicator(tab3).setContent(new Intent(IndexActicity.this,TabActivity3.class)));t.addTab(t.newTabSpec("4").setIndicator(tab4).setContent(new Intent(IndexActicity.this, TabActivity4.class)));tabWidget.setBackgroundColor(getResources().getColor(R.color.alpha_00));tabWidget.setBaselineAligned(true);tab1.setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_bg));for (int i = 0; i < tabWidget.getChildCount(); i++) {tabWidget.getChildAt(i).getLayoutParams().width = Constant.WIDTH / 4;tabWidget.getChildAt(i).getLayoutParams().height=50;}t.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {tabChanged(tabId);}});}//捕获tab变化事件public void tabChanged(String tabId) {if (index_tab != (Integer.valueOf(tabId) - 1)) {tabWidget.getChildAt(Integer.valueOf(tabId) - 1).setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_bg));tabWidget.getChildAt(index_tab).setBackgroundDrawable(null);index_tab = Integer.valueOf(tabId) - 1;}}}
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/testhost" android:layout_height="match_parent"android:layout_width="fill_parent" android:background="@drawable/inde_bg"><LinearLayout android:orientation="vertical" android:id="@+id/layout1"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"></TabWidget><FrameLayout android:id="@android:id/tabcontent"android:layout_width="wrap_content" android:layout_height="wrap_content"></FrameLayout></LinearLayout></TabHost>
public class IndexActicity extends ActivityGroup {private int index_tab = 0;private TabWidget tabWidget;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.index_layout);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);Constant.WIDTH = dm.widthPixels;Constant.HEIGHT= dm.heightPixels;//TabHost t =getTabHost();TabHost t = (TabHost)findViewById(R.id.testhost);t.setBackgroundDrawable(getResources().getDrawable(R.drawable.inde_bg));t.setup(this.getLocalActivityManager());t.setPadding(0, 0, 0, 0);tabWidget = t.getTabWidget();LayoutInflater fi = LayoutInflater.from(IndexActicity.this);View view = fi.inflate(R.layout.tab_layout, null);LinearLayout ll = (LinearLayout) view.findViewById(R.id.tablayout);View tab1 = view.findViewById(R.id.tab1);View tab2 = view.findViewById(R.id.tab2);View tab3 = view.findViewById(R.id.tab3);View tab4 = view.findViewById(R.id.tab4);ll.removeAllViews();t.addTab(t.newTabSpec("1").setIndicator(tab1).setContent(new Intent(IndexActicity.this,TabActivity1.class)));t.addTab(t.newTabSpec("2").setIndicator(tab2).setContent(new Intent(IndexActicity.this,TabActivity2.class)));t.addTab(t.newTabSpec("3").setIndicator(tab3).setContent(new Intent(IndexActicity.this,TabActivity3.class)));t.addTab(t.newTabSpec("4").setIndicator(tab4).setContent(new Intent(IndexActicity.this, TabActivity4.class)));tabWidget.setBackgroundColor(getResources().getColor(R.color.alpha_00));tabWidget.setBaselineAligned(true);tab1.setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_bg));for (int i = 0; i < tabWidget.getChildCount(); i++) {tabWidget.getChildAt(i).getLayoutParams().width = Constant.WIDTH / 4;tabWidget.getChildAt(i).getLayoutParams().height=50;}t.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {tabChanged(tabId);}});}//捕获tab变化事件public void tabChanged(String tabId) {if (index_tab != (Integer.valueOf(tabId) - 1)) {tabWidget.getChildAt(Integer.valueOf(tabId) - 1).setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_bg));tabWidget.getChildAt(index_tab).setBackgroundDrawable(null);index_tab = Integer.valueOf(tabId) - 1;}}