zoukankan      html  css  js  c++  java
  • Android基础——高级UI组件:选项卡

    布局文件

    <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:id="@android:id/tabhost"
        >
    <!--选项卡里面需要两个布局文件:一个是上面标签的布局,一个是下面内容的布局-->
        <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">
            </TabWidget>
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>
        </LinearLayout>
    
    
    </TabHost>

    两个子布局文件

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

    java调用

    package com.example.myhighuiiiii;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.TabHost;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        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();
    
            //加载两个标签页tab1,tab2的布局文件
            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));
        }
    }
  • 相关阅读:
    WEB前端第十九课——雪碧图&滑动门
    近期网上资源收集(一)
    飞利浦 PHILIPS 电动牙刷HX6730 拆解
    webvtt字幕转srt字幕的python程序(附改名程序)
    [转载]Core Elements of a Program
    反正也没人看
    open read split
    蛋疼的二分法死循环
    leetcode ex3 找出穿过最多点的直线 Max Points on a Line
    leetcode AC1 感受
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12233413.html
Copyright © 2011-2022 走看看