zoukankan      html  css  js  c++  java
  • Android选项卡学习

    什么是选项卡


    顶部的导航条就是选项卡了。

    Android开发中添加选项卡的步骤

    图片不太懂上代码:
    activity_main.xml

    <?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"
        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:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </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/bg_left"></ImageView>
    </LinearLayout>
    

    tab2.xml文件和tab1几号一样就是改个id和改个图片就好了。

    MainActivity.java

    package com.example.selectitem;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    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 = (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));
        }
    }
    

    效果:

  • 相关阅读:
    sed中求公共前缀
    DB9 公头母头引脚定义及连接
    J2SE基础:4.面向对象的特性一
    Android APK反编译具体解释(附图)
    wxWidgets刚開始学习的人导引(1)——前言
    使用Visual Studio 2010 创建简单的Silverlight应用程序
    MyEclipse下XFire开发Webservice实例
    实战DeviceIoControl 之中的一个:通过API訪问设备驱动程序
    素数推断算法(高效率)
    Ansi,UTF8,Unicode,ASCII编码的差别
  • 原文地址:https://www.cnblogs.com/lzpq/p/12748516.html
Copyright © 2011-2022 走看看