您的位置:首页 > 移动开发 > Android开发

Android练习-----选项卡(TabHost)、(TabWidget)、(FrameLayout)

2016-10-26 17:46 495 查看
选项卡主要由 TabHost、TabWidget和 FrameLayout 三个控件组成,用于实现一个多标签界面,通过它可以将一个复杂的对话框分割成若干个标签页,实现对信息的分类显示和管理。使用该组件不仅可以使界面简洁大方,还可以有效地减少界面的个数。

在 Android 中,一般实现选项卡的步骤如下:

(1)在布局文件中添加实现选项卡所需的 TabHost、TabWidget 和 FrameLayout 控件。

(2)编写各标签页中要显示内容所对应的 XML 布局文件。

(3)在 Activity 中,获取并初始化 TabHost 控件。

(4)为 TabHost 对象添加标签页

在 Android Stdio 中创建一个 Android 项目,名称为 Ch06_05,实现一个使用选项卡控件的实例。

(1)在项目的 res/layout 目录下修改 activity_main.xml 文件,将其布局文件代码删除,添加实现选项卡所需的 TabHost、TabWidget 和 FrameLayout 控件,其布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"
>

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

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

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabcontent">
</FrameLayout>

</LinearLayout>

</TabHost>


(2)编写第一个标签页要显示内容对应的 XML 布局文件。新建一个 XML 文件,名称为 tab.xml,用于指定第一个标签页要显示的内容,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearLayout01">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1558011110"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="13080111110"/>

</LinearLayout>


(3)编写第二个标签页要显示内容对应的 XML 布局文件。新建一个 XML 布局文件,名称为 tab1.xml,用于指定第二个标签页要显示的内容,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearLayout02">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="136548794211"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="xxxxxxxxxx"/>

</LinearLayout>


(4)在 MainActivity.java 文件中,首先获取到 TabHost 对象后,开始初始化 TabHost, 具体代码如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {

private TabHost tabhost = null;

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

tabhost = (TabHost) findViewById(android.R.id.tabhost);
tabhost.setup();    //初始化TabHost组件

//声明并实例化一个LayoutInflater对象
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.tab,tabhost.getTabContentView());
inflater.inflate(R.layout.tab1,tabhost.getTabContentView());

//添加第一个标签页
tabhost.addTab(tabhost.newTabSpec("tab").setIndicator("未接来电").setContent(R.id.linearLayout01));

//添加第二个标签页
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("已接来电").setContent(R.id.linearLayout02));
}
}


上述代码中,获取到 TabHost 对象后对其进行初始化,然后通过 addTab() 方法来进行添加标签页的操作。

运行该项目:



当选择已接来电时

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