一. Android实现页面跳转有两种方式,一种为.MainActivity跳转;第二种是Relatelayout布局跳转,首先看第一种方式

1. MainActivity区域设置

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取按钮Button button = findViewById(R.id.button);//按钮进行监听button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//监听按钮,如果点击,就跳转Intent intent = new Intent();//前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面intent.setClass(MainActivity.this,NextActivity.class);startActivity(intent);}});}
}

2. 这是下一个页面 的设置

public class NextActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//这个是获取布局文件的,这里是你下一个页面的布局文件setContentView(R.layout.activity_next);}
}

3. 这是第一个页面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/one"android:layout_width="200dp"android:layout_height="100dp"android:text="这是第一个页面!"android:textSize="25dp"android:layout_centerInParent="true"/><Buttonandroid:id="@+id/button"android:layout_width="100dp"android:layout_height="50dp"tools:ignore="MissingConstraints"android:text="跳转"android:layout_centerHorizontal="true"android:layout_below="@+id/one"/></RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

4. 这是第二个页面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><TextViewandroid:id="@+id/two"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是第二个页面!"android:textSize="25dp"android:textColor="#663399"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

5. AndroidManifest.xml配置加上第二个页面的入口

6. 效果图


一. 第二种方式是通过控制Java布局文件进行布局组合

1. 首先MainActivity文件

public class MainActivity extends AppCompatActivity {/*** 声明布局文件* */RelativeLayout layoutTitle,layoutBox,layoutButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取布局文件getwige();}/*** 获取总体布局* */private void getwige() {//获取标题布局getTitles();//获取中间布局getBoxs();//获取底部布局getButtons();}/*** 获取标题布局* */public void getTitles(){//获取总布局中的标题布局layoutTitle = this.findViewById(R.id.title);//初始化一个标题布局类Titles title = new Titles(this);//进行组合布局layoutTitle.addView(title);}/*** 获取标题布局* */public void getBoxs(){//获取总布局中的中间布局layoutBox = this.findViewById(R.id.box);//初始化一个中间布局类Box box = new Box(this);//进行组合布局layoutBox.addView(box);}/*** 获取标题布局* */public void getButtons(){//获取总布局中的底部布局layoutButton = this.findViewById(R.id.button);//初始化一个底部布局类Buttons buttons = new Buttons(this);//进行组合布局layoutButton.addView(buttons);}
}

其相对的主要布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayoutandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="60dp"/><RelativeLayoutandroid:id="@+id/box"android:layout_width="match_parent"android:layout_height="590dp"android:layout_above="@+id/button"android:layout_below="@+id/title" /><RelativeLayoutandroid:id="@+id/button"android:layout_width="match_parent"android:layout_height="80dp"android:layout_alignParentBottom="true"/></RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

2. 首先其他的一些组合布局的类以及其相对布局文件

  1. 标题布局
/*** author:LZH* Date: 2020/6/9* ClassName:Title* Intruduce:标题布局类*/
public class Titles extends RelativeLayout {public Titles(Context context) {super(context);View.inflate(context, R.layout.activity_title,this);}public Titles(Context context, AttributeSet attrs) {super(context, attrs);View.inflate(context, R.layout.activity_title,this);}public Titles(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View.inflate(context, R.layout.activity_title,this);}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="60dp"tools:context=".MainActivity"><RelativeLayoutandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="60dp"tools:ignore="MissingConstraints"android:background="#CCFF00"><TextViewandroid:layout_width="120dp"android:layout_height="30dp"android:layout_centerInParent="true"android:textSize="20dp"android:text="这个是标题"/></RelativeLayout></androidx.constraintlayout.widget.ConstraintLayout>
  1. 中间布局
/*** author:LZH* Date: 2020/6/9* ClassName:Box* Intruduce:中间布局类*/
public class Box extends RelativeLayout {public Box(Context context) {super(context);View.inflate(context, R.layout.activity_box,this);}public Box(Context context, AttributeSet attrs) {super(context, attrs);View.inflate(context, R.layout.activity_box,this);}public Box(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View.inflate(context, R.layout.activity_box,this);}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><RelativeLayoutandroid:id="@+id/box"android:layout_width="match_parent"android:layout_height="590dp"tools:ignore="MissingConstraints"android:background="#6600"><TextViewandroid:layout_width="150dp"android:layout_height="590dp"android:layout_marginTop="450dp"android:layout_centerInParent="true"android:textSize="20dp"android:text="这个是中间布局"/></RelativeLayout></androidx.constraintlayout.widget.ConstraintLayout>
  1. 底部布局
/*** author:LZH* Date: 2020/6/9* ClassName:Button* Intruduce:底部布局类*/
public class Buttons extends RelativeLayout {public Buttons(Context context) {super(context);View.inflate(context, R.layout.activity_button,this);}public Buttons(Context context, AttributeSet attrs) {super(context, attrs);View.inflate(context, R.layout.activity_button,this);}public Buttons(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);View.inflate(context, R.layout.activity_button,this);}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><RelativeLayoutandroid:id="@+id/box"android:layout_width="match_parent"android:layout_height="80dp"tools:ignore="MissingConstraints"android:background="#ccff"><TextViewandroid:layout_width="150dp"android:layout_height="30dp"android:layout_centerInParent="true"android:textSize="20dp"android:text="这个是底部布局"/></RelativeLayout></androidx.constraintlayout.widget.ConstraintLayout>

效果图:

总结,其中第一中方法是真正的跳转方法,而第二中相对于一种组合布局,前者要用到两个或者多个Activity的子类,而后者只需要一个MainActivity。另外,在存在多个Activity的子类时需要设置多个入口,也就是

<activity android:name=".NextActivity"/>

其中,“.”后面是你Activity的子类的名字。

Android实现页面跳转相关推荐

  1. 解决部分android手机页面跳转的黑白屏、闪屏、显示桌面背景问题

    解决部分android手机页面跳转的黑白屏.闪屏.显示桌面背景问题 关于 修改后的方案效果图 问题思路及解决办法 最终方案 关于   今天在查看登录页面美观度的时候意外发现手上的oppo手机在页面跳转 ...

  2. Android intent 页面跳转

    slidemenu_layout 部分列表(点击帮助跳转到user_help_layout.xml页面) //slidemenu_layout.xml <?xml version="1 ...

  3. android h5页面跳转,android H5 应用内跳转Scheme协议

    什么是URL Scheme 概述: android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定义自己的scheme协议,可以非常方便跳转app中的各个页面:通过scheme协议 ...

  4. Android 学习(四)——Android Studio 页面跳转

    一.显式意图跳转 1.设置按键(参考第三次作业:设置按键的三种基本方式) 2.新建Activity2 3.按键侦听,设置页面跳转 Intent intent = new Intent(this,Mai ...

  5. 实现android三页面跳转的简单跳转

    首先创建一个android项目 在res下的layout中对应的activity_main.xml ()写入代码 (背景图片) <LinearLayout xmlns:android=" ...

  6. 实现android多页面跳转,获取数据操作

    准备工具:安卓开发工具 实现页面效果: 1.第一页面:要求停留三秒跳转第二页面 2.第二页面,要求:获取输入的数据点击确定按钮跳转第三页面并数据传入第三页面 3.第三页面:要求:接收第二页面数据显示在 ...

  7. Android Studio页面跳转共享参数

    目录 (-)数据存储 (二)共享参数 1.共享参数概述 2.利用共享参数读写文件步骤 (三)案例演示:多窗口共享数据 1.创建安卓应用 2.准备图片素材 3.主界面类更名 4.创建第二界面类 5.字符 ...

  8. 【Android基础】页面跳转与传值(Activity跳转与传值)

    一个Android应用程序很少会只有一个Activity对象,如何在多个Activity之间进行跳转,而且能够互相传值是一个很基本的要求. 本次我们就讲一下,Android中页面跳转以及传值的几种方式 ...

  9. .Net程序猿玩转Android开发---(11)页面跳转

    在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值 ...

最新文章

  1. python中chr的用法_python中chr()函数和ord()函数的用法
  2. Servlet的Filter的使用
  3. 关于 iOS 10 中 ATS 的问题
  4. matlab生产计划问题,用MATLAB解决综合生产计划编制过程中的优化问题
  5. python读取CIFAR10数据集并将数据集转换为PNG格式存储
  6. REVERSE-PRACTICE-BUUCTF-22
  7. ci框架中引入css,php ci框架中载入css和js文件失败的原因及解决方法
  8. LINQ to XML 编程基础
  9. 关于juniper配速小记
  10. 如何使用digiKam进行照片管理
  11. Solr相关概念详解:SolrRequestHandler
  12. mysql主从复制巡检脚本_mysql主从复制监控shell脚本
  13. zip文件命令 linux,zip命令 – 压缩文件
  14. C++实现人机对战围棋(使用Leela Zero权重)-策略
  15. 绿色版飞信2008 启动时报错“无法注册类别...”的解决办法
  16. 声音文件格式、常见的数字音频格式
  17. Python基础(8)字符串及常用操作
  18. 宕机怎么读?服务器宕机是什么意思?
  19. cati服务器授权信息无效,cati安装
  20. mysql increment_mysql中auto_increment用法详解

热门文章

  1. highChart connectNulls
  2. 对于win10无法运行红警2问题 必有效
  3. 第三章 小程序的数据绑定-从视图中抽离出数据
  4. 微信翻译出Bug上热搜,程序员又背锅?
  5. 学习Linux二(创建、删除文件和文件夹命令)
  6. 【esp8266实践记录】一、使用Arduino IDE 完成为esp8266完成点灯闪烁
  7. 为掌握Java设计模式奠基:两步搞懂UML类图
  8. 文本检测实战:使用OpenCV实现文本检测(EAST 文本检测器)
  9. Vant可靠的移动端组件库
  10. 通过COM接口选中桌面图标