在代码中动态绑定TabHost内容的两种方法(Android)
2010-08-23 16:49
459 查看
TabHost是多标签控件,包含多个Tab,可在一个页面显示多种布局。
使用方法一:
新建Activity继承TabActivity,利用其方法生成TabHost
TabUse1.java
tab1.xml
tab2.xml
方法二:
通过界面定义文件生成TabHost
TabTest.java
TabFactory.java,在此类中可以动态生成各Tab中的控件
main.xml
使用方法一:
新建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>
相关文章推荐
- 在代码中动态绑定TabHost内容的两种方法(Android)
- 用js动态生成css代码的两种方法!
- 使ajax载入动态html内容的代码易于编写及维护的方法
- Android(java)学习笔记229:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)
- 获取android 手机屏幕的大小以及动态设置背景图片的两种方法;
- android 图片叠加效果——两种方法的简介与内容
- Android在布局中动态添加view的两种方法
- Android APK反编译方法(可以获取APK xml和android Manifest,java代码等内容)
- jQuery动态添加的元素绑定事件处理函数代码的方法及比较
- android--解决方案--自定义tabhost(动态添加选项+带自动水平滑动选项卡+手势切换选项卡及内容功能)
- python类的方法属性与方法属性的动态绑定代码详解
- Android APK反编译方法(可以获取APK xml和android Manifest,java代码等内容)
- android 动态添加View 两种方法
- Android APK反编译方法(可以获取APK xml和android Manifest,java代码等内容)
- Android初学习 - 在布局中动态添加view的两种方法
- android在java代码中动态添加组件及相关布局方法(LayoutParams)
- Android 数据库与listview绑定的两种方法
- 【移动安全】Android App Smail代码动态跟踪调试方法
- Java代码中获取配置文件(config.properties)中内容的两种方法
- Android TabWidget/TabHost有两种使用方法