简单选项卡
选项卡其实就是多标签页,选择一个标签就能查看对应的页面
主xml
主要是确定TabHost、TabWidget、TabContent。前两者都有具体的标签,而TabContent我们使用FrameLayout来实现:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_width="match_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" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
子xml
子xml这里只写两个,分别表示两个标签页:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Hello"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Nihao"/>
</LinearLayout>
两个xml其实差不多。
MainActivity
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.setup();
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
inflater.inflate(R.layout.tab2, tabHost.getTabContentView());
tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Hello").setContent(R.id.left));
tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("nihao").setContent(R.id.right));
}
}
首先获取TabHost对象,然后对其进行初始化。
要为TabHost添加标签页,首先需要声明一个LayoutInflater对象,然后加载两个页面,之后把两个标签页添加到tabHost中即可。
效果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DKIunGoS-1629272021862)(C:\Users\15998\AppData\Roaming\Typora\typora-user-images\image-20210818153309265.png)]](https://img-blog.csdnimg.cn/a82bc0da982d433a9cbe083db2fb33aa.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaXlhbnNhbWE=,size_16,color_FFFFFF,t_70)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uUyvs2iC-1629272021863)(C:\Users\15998\AppData\Roaming\Typora\typora-user-images\image-20210818153332879.png)]](https://img-blog.csdnimg.cn/eb0f623260d247faa2b3052a7989d455.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaXlhbnNhbWE=,size_16,color_FFFFFF,t_70)
点击上面的标签即可切换两个页面。
|