android学习之-TabHost

来源:互联网 发布:五轴联动加工中心编程 编辑:程序博客网 时间:2024/05/16 04:41

TabHost的创建有两种方式:

1.使用android系统自带的。

2.自定义TabHost

------------------------------------------

1.系统自带:

main.xml

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android: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"  android:padding="5dp">  <TabWidget android:id="@android:id/tabs" android:layout_marginLeft="10dp"   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"   android:padding="5dp" /> </LinearLayout></TabHost>

注意事项:上面的id要包括:id="@android:id/tabhost";:id="@android:id/tabs";id="@android:id/tabcontent"

activity:

创建一个类继承TabActivity

public class SettingActivity extends TabActivity{

setContentView(R.layout.main);

  Resources res = getResources();   TabHost tabHost = getTabHost();    TabHost.TabSpec spec;  Intent intent;  //1.设置  intent = new Intent(this,RegistActivity.class);  spec = tabHost.newTabSpec("artists").setIndicator("",       res.getDrawable(R.drawable.regist))            .setContent(intent);         tabHost.addTab(spec);  //2.选择  intent = new Intent(this,SelectActivity.class);  spec = tabHost.newTabSpec("artists").setIndicator("",       res.getDrawable(R.drawable.select))            .setContent(intent);         tabHost.addTab(spec);  //3.联系  intent = new Intent(this,ContactActivity.class);  spec = tabHost.newTabSpec("artists").setIndicator("",       res.getDrawable(R.drawable.contact))            .setContent(intent);         tabHost.addTab(spec);

}

 

2.自定义TabHost

1.main.xm

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/main_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"  android:padding="5dp">  <TabWidget android:id="@android:id/tabs" android:layout_marginLeft="10dp"   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"   android:padding="5dp" /></LinearLayout></TabHost>

注意事项:

使用到的两个id是系统的

class文件:

public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_tabhost);        tabHost=(TabHost)findViewById(R.id.TabHost01);        tabHost.setup();

        tabHost.addTab(tabHost.newTabSpec("tab1").setContent(R.id.LinearLayout1).setIndicator("aa"));        tabHost.addTab(tabHost.newTabSpec("tab2").setContent(R.id.LinearLayout2).setIndicator("bb"));

        tabHost.setCurrentTab(0);    }

--------------------------------------------------------------

自己定义图片之间的一些参数:

//设置高度和padding  int childCount = tabHost.getTabWidget().getChildCount();  for(int i = 0;i<childCount;i++) {   tabHost.getTabWidget().getChildAt(i).setPadding(0, 0, 0, 0);   tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;     }

原创粉丝点击