效果
准备工作
因为我们的每一个选项卡对应的都是一个xml布局文件,所以我们先直接写好两个xml布局文件,就不再在TabContent中添加了(在TabContent中直接添加的话,组件多了会很乱).
布局l: 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:id="@+id/left"
android:orientation="vertical">
<ImageView
android:src="@drawable/lufei"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
布局2:tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/right"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:src="@drawable/kakaxi"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
因为在布局中用到了两张图片,所以要提前在drawable文件夹中放入两张图片
.xml文件
**.xml布局文件要注意的地方
- TabHost不能理解为一个布局, 应该理解为一个选项卡容器, 这个容器又包含两部分: 选项卡的标题部分(上图的"路飞"), 选项卡的内容部分(上图的图片). 其中TabWidget 是用来存放选项卡标签的, 而FrameLayout是用来存放选项卡内容的.
- TabHost, TabWidget, FrameLayout的id和以往的UI组件不一样, 它们三个必须使用安卓指定的id名称, 并且是以@android开头. 三者分别对应 :"@android:id/tabhost" “@android:id/tabs” “@android:id/tabcontent”
- TabWidget和FrameLayout的顺序如果颠倒,可能会导致程序无法正常运行.具体怎么解决还请自行百度.
4.TabWidget和FrameLayout必须要用LinerLayout布局文件框起来, 因为LinearLayout才有weight属性,这个属性代表的是比重,android中所有的组件默认这个属性都是为0,意味着他们只在屏幕上显示他们需要空间的大小。如果设置了该属性,activity将根据设置的值按比例来划分空间的大小.
**
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
.java文件
package com.example.newapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = findViewById(android.R.id.tabhost);//获取TabHost
tabHost.setup();//对Tablhost进行初始化
LayoutInflater inflater = LayoutInflater.from(this);//这里的LayoutInflater类型的变量是用于加载布局的
inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
//以上调用它的inflate()方法来加载布局,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就传入null.这里我们需要把布局文件添加到FrameLayout(TabContent)中,所以利用getTabContentView()方法来传入.
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("路飞").setContent(R.id.left));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("漂亮啊").setContent(R.id.right));
//最后两句是向TabHost添加选项卡.还可以这样写
/*TabHost.TabSpec tabSpec = tabHost.newTabSpec("tab1");//给选项卡设置tag(标记)
tabSpec.setIndicator("路飞");, //设置选项卡的标签名
tabSpec.setContent(R.id.left);//为选项卡设置指定的布局文件
tabHost.addTab(tabSpec);//将次选项卡添加到TabHost中 */
}
}
PS:如果想更加深入了解LayoutInflater 的原理可以看一下郭神的博客:LayoutInflater原理分析跳转网址