Android UI设计 选项卡实现界面切换

本文介绍了如何在Android应用中使用FrameLayout和TabHost组件实现界面的切换功能,通过创建多个TabSpec对象并设置不同的Intent来展示不同界面,同时提供了对应的XML布局文件以实现界面的显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用选项卡实现界面切换

一、

      FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。

1、MainActivity.java

package com.example.ui1;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tabHost = getTabHost();
		AddTabPage1();
		AddTabPage2();
		AddTabPage3();

	}

	private void AddTabPage1() {
		// TODO Auto-generated method stub
		Intent intent1 = new Intent();
		intent1.setClass(this, Act1.class);
		TabSpec tabSpec = tabHost.newTabSpec("act1");
		tabSpec.setIndicator("", getResources().getDrawable(R.drawable.to));
		tabSpec.setContent(intent1);
		tabHost.addTab(tabSpec);

	}

	private void AddTabPage2() {
		// TODO Auto-generated method stub
		Intent intent2 = new Intent();
		intent2.setClass(this, Act2.class);
		TabSpec tabSpec = tabHost.newTabSpec("act2");
		tabSpec.setIndicator("", getResources().getDrawable(R.drawable.tt));
		tabSpec.setContent(intent2);
		tabHost.addTab(tabSpec);

	}

	private void AddTabPage3() {
		// TODO Auto-generated method stub
		Intent intent3 = new Intent();
		intent3.setClass(this, Act3.class);
		TabSpec tabSpec = tabHost.newTabSpec("act3");
		tabSpec.setIndicator("", getResources().getDrawable(R.drawable.t3));
		tabSpec.setContent(intent3);
		tabHost.addTab(tabSpec);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

2、Act.java

package com.example.ui1;

import android.app.Activity;
import android.os.Bundle;

public class Act1 extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_act1);

	}

}

3、Act2.java

package com.example.ui1;

import android.app.Activity;
import android.os.Bundle;

public class Act2 extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_act2);

	}

}

4、Act3.java

package com.example.ui1;

import android.app.Activity;
import android.os.Bundle;

public class Act3 extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_act3);

	}

}

二、

1、activity_main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="1dip"
            android:paddingRight="1dip"
            android:paddingTop="4dip" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:layout_weight="1" />
    </LinearLayout>

</TabHost>

2、layout_act1.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/a"
        android:text="第一个窗体" />

</LinearLayout>

3、layout_act2.xml

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/b"
        android:text="第二个窗体" />

</LinearLayout>

4、layout_act3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/c"
        android:text="第三个窗体" />
    

</LinearLayout>

三、效果图









好的,下面为您介绍一个简单的Spring Boot + Mybatis Plus(SSMP)整合案例。 1. 首先,在pom.xml文件中添加以下依赖: ```xml <!--Spring Boot 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Mybatis Plus 依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> <!--MySQL 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> ``` 2. 在application.properties中配置数据源: ```properties # 数据库配置 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 创建一个实体类User,用于映射数据库中的user表: ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 4. 创建UserMapper接口,用于定义数据库操作: ```java public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建UserService类,用于定义业务逻辑: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> selectList() { return userMapper.selectList(null); } public User selectById(Long id) { return userMapper.selectById(id); } public int insert(User user) { return userMapper.insert(user); } public int update(User user) { return userMapper.updateById(user); } public int delete(Long id) { return userMapper.deleteById(id); } } ``` 6. 创建UserController类,用于处理HTTP请求: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public List<User> selectList() { return userService.selectList(); } @GetMapping("/{id}") public User selectById(@PathVariable Long id) { return userService.selectById(id); } @PostMapping public int insert(@RequestBody User user) { return userService.insert(user); } @PutMapping public int update(@RequestBody User user) { return userService.update(user); } @DeleteMapping("/{id}") public int delete(@PathVariable Long id) { return userService.delete(id); } } ``` 7. 启动应用程序,访问http://localhost:8080/user/list可以获取所有用户的列表,访问http://localhost:8080/user/1可以获取id为1的用户的详细信息。 以上就是一个简单的Spring Boot + Mybatis Plus(SSMP)整合案例,希望能帮助到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值