在代码中动态绑定TabHost内容的两种方法(Android)

TabHost是多标签控件,包含多个Tab,可在一个页面显示多种布局。

 

使用方法一:

新建Activity继承TabActivity,利用其方法生成TabHost

TabUse1.java

package org.tabhost; import org.tabhost.R; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.*; public class TabUse1 extends TabActivity { private TabHost tabhost; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tabhost=getTabHost(); LayoutInflater layoutInflater=LayoutInflater.from(this); layoutInflater.inflate(R.layout.tab1, tabhost.getTabContentView()); FrameLayout frameLayout=(FrameLayout) findViewById(R.id.tab1); TextView textView=new TextView(this); textView.setText("tab1-TextView"); frameLayout.addView(textView); TabHost.TabSpec tabSpec1=tabhost.newTabSpec("tab-1"); tabSpec1.setIndicator("tab-1", null); tabSpec1.setContent(R.id.tab1); tabhost.addTab(tabSpec1); layoutInflater.inflate(R.layout.tab2, tabhost.getTabContentView()); LinearLayout lineLayout=(LinearLayout) findViewById(R.id.tab2); Button btn=new Button(this); btn.setText("tab2-Button"); lineLayout.addView(btn); TabHost.TabSpec tabSpec2=tabhost.newTabSpec("tab-2"); tabSpec2.setIndicator("tab-2", null); tabSpec2.setContent(R.id.tab2); tabhost.addTab(tabSpec2); } } 

tab1.xml

<?xml version="1.0" encoding="UTF-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab1" > </FrameLayout> 

tab2.xml

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/tab2"> </LinearLayout> 

方法二:

通过界面定义文件生成TabHost

TabTest.java

package org.tabhost; import android.app.Activity; import android.os.Bundle; import android.widget.TabHost; public class TabTest extends Activity { /** Called when the activity is first created. */ /** Called when the activity is first created. */ private TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try{ tabHost = (TabHost) this.findViewById(R.id.TabHost01); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab_1") .setContent(new TabFactory(this)) .setIndicator("TAB1")); tabHost.addTab(tabHost.newTabSpec("tab_2") .setContent(new TabFactory(this)) .setIndicator("TAB2")); tabHost.addTab(tabHost.newTabSpec("tab_3") .setContent(new TabFactory(this)) .setIndicator("TAB3")); tabHost.setCurrentTab(1); }catch(Exception ex){ ex.printStackTrace(); } }

TabFactory.java,在此类中可以动态生成各Tab中的控件

package org.tabhost; import android.content.Context; import android.view.View; import android.widget.EditText; import android.widget.TabHost.TabContentFactory; public class TabFactory implements TabContentFactory { private Context con; public TabFactory(Context c){ con=c; } @Override public View createTabContent(String arg0) { EditText text=new EditText(con); text.setText("text1"); return text; } } 

main.xml

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TabHost01" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" 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"> </FrameLayout> </LinearLayout> </TabHost>  

转载于:https://www.cnblogs.com/whuqin/archive/2010/08/23/4982139.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值