android学习之-TabHost

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;       }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值