【安卓】选项卡之顶部选项卡(简易)

一、顶部选项卡

1.方法:(1)在res的layout文件目录下新建.xml的布局文件,并取名为:main_tab.xml(自己喜好,随意发挥)。

              (2)在src文件夹里的包下创建Java类,名为TabHostActivity(自己喜好,随意发挥)。

2.布局文件代码:

<?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"
    android:background="#000066" >

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

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

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

            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="主页--第一页"
                android:textSize="11pt" />

            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="精选--第二页"
                android:textSize="11pt" />

            <TextView
                android:id="@+id/tv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="关于--第三页"
                android:textSize="11pt" />
        </FrameLayout>
    </LinearLayout>

</TabHost>

3.写好布局代码之后的效果图

4.Java代码

package com.xuanxiang;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class TabHostActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab);
		TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
		// 必须调用该方法,才能设置tab样式
		tabhost.setup();
		// 添加标签tab1
		tabhost.addTab(tabhost
				.newTabSpec("主页")
				// 设置tab1标签图片
				.setIndicator(null,
						getResources().getDrawable(R.drawable.ic_launcher))
				// 设置tab1内容
				.setContent(R.id.tv1));

		// 添加标签tab2
		tabhost.addTab(tabhost
				.newTabSpec("精选")
				// 设置tab2标签图片
				.setIndicator(null,
						getResources().getDrawable(R.drawable.ic_launcher))
				// 设置tab2内容
				// .setContentView(R.layout.main_tab);
				.setContent(R.id.tv2));

		// 添加标签tab3
		tabhost.addTab(tabhost
				.newTabSpec("关于")
				// 设置tab1标签图片
				.setIndicator(null,
						getResources().getDrawable(R.drawable.ic_launcher))
				// 设置tab3内容
				.setContent(R.id.tv3));
		tabhost.setCurrentTab(0);// 设置当前显示第一个tab
	}
}

5.大功告成之后,Android虚拟机运行的效果图

6.如果在虚拟机运行报错,请检查是否在AndroidManifest.xml里注册Activity;以及注册的Activity名称是否一致。

7.本人QQ:768946914,欢迎添加

猜你喜欢

转载自blog.csdn.net/weixin_42449711/article/details/80981263
今日推荐