关闭

TabLayout简单例子

3748人阅读 评论(0) 收藏 举报
要生成一个Tab UI需要用到两个类,一个是TabHost,一个是TabWidget. TabWidget是用来显示标签栏的,内嵌在TabHost里面。

首先创建以TabHost为根节点的XMl布局文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- 将Layout的XML文件的根节点设置为TabHost,并且id要为Android内置的id:@android:id/tabhost -->
<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" >

    <!-- 将TabHost里面的布局设置为线性布局 -->


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >

        <!-- 设置TabHost里面的TabWidget,放置标签,id要设置为Android内置的id: @android:id/tabs -->

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <!-- 设置TabHost里面的FrameLayout,放置内容,id设置为Android是内置id: @android:id/tabcontent -->

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </FrameLayout>
    </LinearLayout>

</TabHost>
生成三个对应于标签内容的Activity:

PictureActivity:

public class PictureActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		System.out.println("PictureActivity--->onCreate");
		super.onCreate(savedInstanceState);

		LinearLayout layout = new LinearLayout(this);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		ImageView imgView = new ImageView(this);
		imgView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		imgView.setImageResource(R.drawable.girl);
		layout.addView(imgView);

		setContentView(layout);
	}
}
TextActivity:

public class TextActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		System.out.println("TextActivity--->onCreate");
		super.onCreate(savedInstanceState);

		LinearLayout layout = new LinearLayout(this);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		TextView textView = new TextView(this);
		textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		textView.setText("你好,这是个美女信息");
		layout.addView(textView);
		setContentView(layout);
	}
}
EditActivity:

public class EditActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		System.out.println("EditActivity--->onCreate");
		super.onCreate(savedInstanceState);

		LinearLayout layout = new LinearLayout(this);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		EditText edit = new EditText(this);
		edit.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		layout.addView(edit);

		setContentView(layout);
	}
}
主要的Activity:

public class MyTab extends TabActivity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 得到xml里面定义的TabHost
		TabHost host = getTabHost();
		// 开始生成三个标签,分别对应PictureActivity,TextActivity,EditActivity

		TabHost.TabSpec label1 = host.newTabSpec("picture");
		// 设置标签的图片及信息
		label1.setIndicator("相片", getResources().getDrawable(R.drawable.label1));
		// 设置便签对应显示内容
		label1.setContent(new Intent(this, PictureActivity.class));
		host.addTab(label1);

		// 同样设置另外两个标签
		TabHost.TabSpec label2 = host.newTabSpec("information");
		label2.setIndicator("个人信息", getResources().getDrawable(R.drawable.label2));
		label2.setContent(new Intent(this, TextActivity.class));
		host.addTab(label2);

		TabHost.TabSpec label3 = host.newTabSpec("edit");
		label3.setIndicator("编辑", getResources().getDrawable(R.drawable.label3)).setContent(new Intent(this, EditActivity.class));
		host.addTab(label3);

		host.setCurrentTab(0);
	}
}


TabWidget会在TabHost调用addTab()时候,系统自动将新增的Tab加进TabWidget中,这个不用我们来操作。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89366次
    • 积分:780
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:6篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论