Android studio 选项卡

先看主XMl文件,
··· <在布局文件添加TabHost,TabWidget,TabContent组件,
三个组件的ID都是预定义好的@android:id/***
前两个有固定的标签,最后一个通常是FrameLayout>
···<编写各个标签页的XML文件>
··· <获取并初始化TabHost组件>
··· <为TabHost对象增加标签页>

<?xml version="1.0" encoding="utf-8"?>
<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:id="@android:id/tabhost"
    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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs"></TabWidget>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
    
</TabHost>

编写其余的选项卡xml文件
tab1.xml文件(aa1是图片资源)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/aa1"/>
</LinearLayout>

tab2.xml文件(bb1是图片资源)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/right"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bb1"/>
</LinearLayout>

…可以继续其他的选项控件xml…不做多余的说明,我在主函数用了三个选项控件

看activity函数


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);![在这里插入图片描述](https://img-blog.csdnimg.cn/20190820200443835.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTg2MDUx,size_16,color_FFFFFF,t_70)
        TabHost tabHost=findViewById(android.R.id.tabhost);
        tabHost.setup();//初始化,必须调用
        //添加标签页,需要声明实例化一个inflater对象
        LayoutInflater inflater=LayoutInflater.from(this);
        //加载布局文件
        inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
        inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
        inflater.inflate(R.layout.tab3,tabHost.getTabContentView());
        //三个参数,指定标签页,文字内容,设置内容
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("天理").setContent(R.id.right));
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("大虾").setContent(R.id.center));
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("石头").setContent(R.id.left));
    }
}

点击不同的标签,就到不同的选项界面

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值