选项卡

选项卡组件不能直接添加到xml中。

  • 1、在布局文件中添加TabHost、TabWidget和TabContent组件
    前面两个组件都有相应的标签,而TabContent通常通过FrameLayout来实现。
  • 2、编写和标签页的xml布局文件
  • 3、在activity中获取并初始化TabHost组件
  • 4、为TabHost对象添加标签页

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/tabhost"
        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:layout_width="match_parent" android:layout_height="wrap_content"
                     android:id="@android:id/tabcontent">

        </FrameLayout>
    </LinearLayout>

</TabHost>

tab1.xml:

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

    <ImageView android:layout_width="match_parent" android:layout_height="match_parent"
               android:src="@drawable/panda"/>

</LinearLayout>

tab2.xml:

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

    <ImageView android:layout_width="match_parent" android:layout_height="match_parent"
               android:src="@drawable/laugh"/>

</LinearLayout>

java:

package com.example.tabhost;

import android.view.LayoutInflater;
import android.widget.TabHost;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

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("精选表情").setContent(R.id.left));
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("投稿表情").setContent(R.id.right));

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值