您的位置:首页 > 其它

TabHost之心得

2014-01-26 19:46 316 查看
MainActivity:

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private TabHost tabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("nihao").setIndicator("Tab1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.widget_layout_Blue));
tabHost.addTab(tabHost.newTabSpec("nihao").setIndicator("Tab2",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.widget_layout_green));
tabHost.addTab(tabHost.newTabSpec("nihao").setIndicator("Tab3",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.widget_layout_red));

}


 

主布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:id="@+id/widget_layout_Blue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 静态加载布局 -->
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/tab1" />
</LinearLayout>

<LinearLayout
android:id="@+id/widget_layout_red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/tab3" />
</LinearLayout>

<LinearLayout
android:id="@+id/widget_layout_green"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/tab2" />
</LinearLayout>

</FrameLayout>


 

tab布局文件分别为tab1,tab2,tab3 使用<include/>标签可以静态加载tab布局,更方便布局(主布局中的LinearLayout可以为相对布局)


tab1使用的是RelativeLayout布局
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tabhost