Android选项卡学习
2024-09-05 19:58:11
什么是选项卡
顶部的导航条就是选项卡了。
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));
}
}
效果:
最新文章
- 11.12模拟考T1(可持续优化)PS:神奇的东西
- ecshop商品详细描述调用商品相册代码
- js知识点 知识树 知识结构 (转载 学习中)
- JavaScript学习笔记(十二) 回调模式(Callback Pattern)
- [Javascript] The JSON.stringify API
- 使用Net.Mail、CDO组件、JMail组件三种方式发送邮件
- .net中,控件(Name)属性或ID属性的常见命名规则
- Mego开发文档 - 复杂保存操作
- Cisco 关闭命令同步提示信息
- php 版本升高后 会出现 之Deprecated: Function ereg_replace() is deprecated的解决方法
- 微信小程序云开发更换云开发环境
- Go Web:Handler
- Python函数学习——作用域与嵌套函数
- 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作
- Windows下Oracle创建数据库的3种方式
- Spring Batch 基本的批处理指导原则
- 什么是XP
- 多态、抽象类、接口_DAY09
- PTA (Advanced Level) 1004 Counting Leaves
- Node.js实战(四)之调试Node.js
热门文章
- 使用Buildpacks高效构建Docker镜像
- SpringMVC中使用@Valid和BindingResult进行参数验证
- 解码问题--leetcode:91 (2019商汤笔试)
- [ES6系列-01]Class:面向对象的“新仇旧恨”
- [代码片段-C#]工具代码片段 及 版本信息等
- RxJS 中的创建操作符
- Rocket - tilelink - Atomics
- (二)用less+gulp+requireJs 搭建项目(gulp)
- Java实现蓝桥杯 算法提高 八皇后 改
- Java实现 LeetCode 605 种花问题(边界问题)