android TabHost选项卡示例

匿名 (未验证) 提交于 2019-12-02 23:43:01

1. 继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布局文件中定义即可。

tabactivity.xml:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent" >          <LinearLayout android:id="@+id/firstTab"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:gravity="center_horizontal"         android:orientation="vertical">                  <TextView android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:text="这是第一个选项卡"             android:textSize="20px"/>              </LinearLayout>          <LinearLayout android:id="@+id/secondTab"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:gravity="center_horizontal"         android:orientation="vertical">                  <TextView android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:text="这是第二个选项卡"             android:textSize="20px"/>              </LinearLayout>           <LinearLayout android:id="@+id/thirdTab"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:gravity="center_horizontal"         android:orientation="vertical">                  <TextView android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:text="这是第三个选项卡"             android:textSize="20px"/>              </LinearLayout>  </FrameLayout>

2. 不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的ID必须是 @android :id/tabs,FrameLayout的ID必须是 @android :id/tabcontent,TabHost的ID可自定义。

tabxml.xml:

<?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:orientation="vertical" >          <TextView android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="使用布局文件中定义TabHost的方式实现TabHost"         android:textSize="15px"/>     <TabHost android:id="@+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:orientation="horizontal"                 android:layout_width="fill_parent"                 android:layout_height="wrap_content">             </TabWidget>             <FrameLayout android:id="@android:id/tabcontent"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent">                 <TextView android:id="@+id/view1"                     android:layout_width="fill_parent"                		android:layout_height="fill_parent"                		android:text="这是第一个选项卡"                		android:textSize="20px"/>                 <TextView android:id="@+id/view2"                     android:layout_width="fill_parent"                		android:layout_height="fill_parent"                		android:text="这是第二个选项卡"                		android:textSize="20px"/>                 <TextView android:id="@+id/view3"                     android:layout_width="fill_parent"                		android:layout_height="fill_parent"                		android:text="这是第三个选项卡"                		android:textSize="20px"/>                              </FrameLayout>         </LinearLayout>     </TabHost>  </LinearLayout>

MyTabActivity.java 文件:

package com.example.baseexample;  import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost;  public class MyTabActivity extends TabActivity { 	private TabHost myTabHost ; 	 	protected void onCreate(Bundle savedInstanceState){ 		super.onCreate(savedInstanceState); 		myTabHost = this.getTabHost(); 		LayoutInflater.from(this).inflate(R.layout.tabactivity, myTabHost.getTabContentView(),true); 		myTabHost.addTab(myTabHost.newTabSpec("选项卡1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.firstTab)); 		myTabHost.addTab(myTabHost.newTabSpec("选项卡2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.secondTab)); 		myTabHost.addTab(myTabHost.newTabSpec("选项卡3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.thirdTab)); 	} }

TabXmlActivity.java文件:

package com.example.baseexample;  import android.app.Activity; import android.os.Bundle; import android.widget.TabHost;  public class TabXmlActivity extends Activity { 	 	protected void onCreate(Bundle savedInstanceState){ 		super.onCreate(savedInstanceState); 		setContentView(R.layout.tabxml); 		TabHost tabHost = (TabHost)findViewById(R.id.tabhost); 		tabHost.setup(); 		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1)); 		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view2)); 		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view3)); 	} }



转载于:https://my.oschina.net/u/2552902/blog/543842

转载请标明出处:android TabHost选项卡示例
文章来源: https://blog.csdn.net/weixin_33748818/article/details/92326912
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!