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

一、底部选项卡

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

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

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" >

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0" >

            <Button
                android:id="@+id/b1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="第一页" />

            <Button
                android:id="@+id/b2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="第二页" />

            <Button
                android:id="@+id/b3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="第三页" />
        </FrameLayout>

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

</TabHost>

3.布局文件写好后的效果图:

4.Java代码

package com.xuanxiang;

import android.os.Bundle;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class Main_tab extends TabActivity {

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

		TabHost tabHost = getTabHost();

		TabSpec page1 = tabHost
				.newTabSpec("tab1")
				.setIndicator("联系人",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.b1);
		tabHost.addTab(page1);

		TabSpec page2 = tabHost
				.newTabSpec("tab2")
				.setIndicator("消息",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.b2);
		// setContentView(R.layout.main_tab);
		// .setContent(R.id.spiderman);
		tabHost.addTab(page2);

		TabSpec page3 = tabHost
				.newTabSpec("tab3")
				.setIndicator("动态",
						getResources().getDrawable(R.drawable.ic_launcher))
				.setContent(R.id.b3);
		tabHost.addTab(page3);
	}
}


5.完成过后,Android虚拟机运行的效果图


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


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


猜你喜欢

转载自blog.csdn.net/weixin_42449711/article/details/80981966