文章目录

  • 2.1 View控件
  • 2.2 界面布局编写方式
    • XML中编写
    • Java中编写
  • 2.3 界面布局的通用属性
    • android:id
    • android:layout_width&&android:layout_height
    • android:backgroud
    • android:layout_margin&&android:padding
  • 2.4 线性布局
    • 2.4.1 线性布局简介
      • 设置控件权重举例
    • 2.4.2 实战演练—仿动物连连看游戏界面
      • themes.xml
      • activity_main.xml
  • 2.5 相对布局
    • 2.5.1 相对布局RelativeLayout简介
      • 相对布局举例
    • 2.5.2 实战演练——音乐播放器界面
  • 2.6表格布局
      • 表格布局举例
    • 2.6.2 实战演练-计算机界面
  • 2.7 帧布局
      • 帧布局举例
    • 2.7.2 实战演练-霓虹灯界面

2.1 View控件

Android的所有UI元素都是通过View控件与ViewGroup容器构建的,ViewGroup作为一个容器装下所有界面中的控件

注:Android应用程序中的每一个界面有且只有一个ViewGroup容器

2.2 界面布局编写方式

XML中编写

多采用这种方式编写,有效的与Java代码相互隔离,使得整个程序的结构更加清晰

可在res/layout文件夹中编写相应的界面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"><Buttonandroid:id="@+id/btn_textview"android:layout_width="match_parent"android:layout_height="wrap_content"android:textAllCaps="false"android:text="TextView"/><Buttonandroid:id="@+id/btn_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Button"android:textColor="@color/black"android:textAllCaps="false"/><Buttonandroid:id="@+id/btn_edittext1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView"android:textAllCaps="false"/></LinearLayout>

Java中编写

通过new关键字创键控件,将创键好的View控件添加到ViewGroup容器中,从而实现了在布局界面中显示View控件

        RelativeLayout ralativelayout = new RelativeLayout(this);//创键RelativeLayout对象RelativeLayout.LayoutParams params = new              RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(RelativeLayout.CENTER_IN_PARENT);//设置居中显示,至此LayoutParams创键完成TextView textview = new TextView(this);textview.setText("text");ralativelayout.addView(textview,params);//将两个对象装进RelativeLayout中setContentView(ralativelayout);//设置在Activity中显示

2.3 界面布局的通用属性

Android 提供的四种常用布局继承自ViewGroup,具有ViewGroup中的一些属性

android:id="@+id/属性名称"   设置布局标识
android:layout_width=""      设置布局宽度
android:layout_height="" 设置布局高度
android:background=""        设置布局背景
android:layout_margin="" 设置margin
android:padding=""           设置padding

android:id

该属性是当前布局的唯一标识,,XML中通过@+id/属性名称来定义,设置好属性后会在R.java中生成对应的int值,在Java代码中可以通过findViewById()方法传入该int值来获取该布局的对象

mBtnTextView = (Button) findViewById(R.id.btn_textview);

android:layout_width&&android:layout_height

设置布局的宽高

还可以选用系统定义的值:

  1. fill_parent:表示该布局的高度和父容器的高度相同
  2. match_parent:与前者相同,更推荐这种方式
  3. wrap_content:匹配所包含的内容

android:backgroud

设置布局背景,可以是颜色,可以是图片

android:layout_margin&&android:padding

与HTML类似

2.4 线性布局

<LinearLayout>内容</LinearLayout>

2.4.1 线性布局简介

  • android:orientation:设置布局内控件的排列顺序

    属性值:vertical(垂直排列),horizontal(水平排列)

  • android:layout_weight:设置空间的权重,可以使布局内的控件按照权重比显示大小,对屏幕适配起关键作用

设置控件权重举例

    <LinearLayout android:layout_below="@id/Btn_4"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="0dp"android:text="按钮1"android:layout_weight="1"android:layout_height="wrap_content"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="按钮2"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:text="按钮3"android:layout_weight="2"/></LinearLayout>

设置三个Button控件,宽度为0dp,设置权重分别为1,1,2,实际宽度之比为1:1:2

2.4.2 实战演练—仿动物连连看游戏界面

themes.xml

<style name="btnStyle"><item name="android:layout_weight">70</item><item name="android:layout_height">70dp</item><item name="android:layout_marginRight">15dp</item>
</style>

activity_main.xml

    <LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonstyle="@style/btnStyle"android:background="@drawable/a"/><Buttonstyle="@style/btnStyle"android:background="@drawable/b"/><Buttonstyle="@style/btnStyle"android:background="@drawable/c"/></LinearLayout>

此题主要是对图片进行排版

注意点:

  1. 图片必须以字母开头,不能有中文,不然会报错
  2. 图片放在drawable-hdpi文件夹中

2.5 相对布局

<RelativeLayout>内容</RelativeLayout>

2.5.1 相对布局RelativeLayout简介

RelativeLayout以父容器或者其他子控件作为参照物,指定布局中子控件的位置,相关属性如图

相对布局举例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/white"android:text="按钮1"android:layout_alignParentBottom="true"android:layout_marginBottom="10dp"android:textSize="30sp"/><Buttonandroid:id="@+id/bt_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/white"android:layout_centerInParent="true"android:text="按钮1"android:textSize="30sp"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/white"android:layout_alignParentRight="true"android:layout_alignBottom="@+id/bt_1"android:text="按钮1"android:layout_marginRight="20dp"android:layout_marginBottom="100dp"android:textSize="30sp"/>
</RelativeLayout>

注:为了让Android有更好的屏幕适应能力,最好使用wrap_parent,match_parent,尽量避免长宽设置为一个固定的值

2.5.2 实战演练——音乐播放器界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"tools:context=".MainActivity"><RelativeLayoutandroid:id="@+id/head"android:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:layout_width="20dp"android:layout_height="20dp"android:background="@drawable/q" /><Buttonandroid:layout_width="20dp"android:layout_height="20dp"android:background="@drawable/w"android:layout_alignParentRight="true" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:textSize="20sp"android:layout_centerHorizontal="true"android:text="这里是音乐名"/></RelativeLayout><Buttonandroid:id="@+id/imga"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="@drawable/s" /><RelativeLayoutandroid:id="@+id/foot"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginBottom="80dp"android:layout_alignParentBottom="true"><Buttonandroid:id="@+id/bt11"android:background="@drawable/w"android:layout_width="40dp"android:layout_height="40dp"/><Buttonandroid:id="@+id/bt22"android:layout_toRightOf="@id/bt11"android:background="@drawable/w"android:layout_marginRight="20dp"android:layout_marginLeft="20dp"android:layout_width="40dp"android:layout_height="40dp"/><Buttonandroid:id="@+id/bt33"android:layout_toRightOf="@id/bt22"android:background="@drawable/w"android:layout_width="40dp"android:layout_height="40dp" /></RelativeLayout></RelativeLayout>

没有相应图片,就随便找了几个图片,弄了大概的样子(中间是专辑图,下边是播放下一曲上一曲)

注:Button颜色或者背景图无法改变的解决办法:

改themes.xml中的Style的属性parent="Theme.MaterialComponents.DayNight.Bridge"

2.6表格布局

    <TableLayout>内容</TableLayout>

表格布局继承自线性布局类,完全支持线性布局的属性,其他属性如图

注意点:列的宽度由该列中最大的单元格决定,整个表格的宽度取决于父组件的宽度

表格布局举例

    <TableLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_column="2"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/></TableRow></TableLayout>

2.6.2 实战演练-计算机界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"tools:context=".MainActivity"><TableLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="<-"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_span="2"android:text="0"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_span="2"android:text="="/></TableRow></TableLayout>
</RelativeLayout>

2.7 帧布局

<FrameLayout></FrameLayout>

帧布局用于在屏幕上创建一块空白区域,添加到该区域中的每一个子控件占一帧,每一个帧都会覆盖在上一个帧的上面

俩属性

  • android:foreground:设置FrameLayout容器的前景图像,始终显示在子控件上面
  • android:foreground:设置前景图像显示的位置

帧布局举例

    <FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:layout_width="200dp"android:layout_height="200dp"/><Buttonandroid:layout_width="50dp"android:layout_height="50dp"android:background="#000000" /></FrameLayout>

2.7.2 实战演练-霓虹灯界面

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/btn_one"android:layout_width="300dp"android:layout_height="300dp"android:layout_gravity="center"android:background="#ff0000" /><Buttonandroid:id="@+id/btn_two"android:layout_width="220dp"android:layout_height="220dp"android:layout_gravity="center"android:background="#00ff00" /><Buttonandroid:id="@+id/btn_three"android:layout_width="140dp"android:layout_height="140dp"android:layout_gravity="center"android:background="#0000ff" /><Buttonandroid:id="@+id/btn_four"android:layout_width="80dp"android:layout_height="80dp"android:layout_gravity="center"android:background="#ff1243" /><Buttonandroid:id="@+id/btn_five"android:layout_width="20dp"android:layout_height="20dp"android:layout_gravity="center"android:background="#324678" />
</FrameLayout>

(2)Android常见界面布局相关推荐

  1. Android常见界面布局

    第2章 Android常见界面布局 第2章 Android常见界面布局 2.1 View视图 2.2 界面布局编写方式 2.2.1 在XML文件中编写布局 2.2.2 在Java代码中编写布局 2.3 ...

  2. Android常见界面布局(详细介绍)

    一.View视图 所有的UI元素都是通过View与ViewGroup构建的,对于一个Android应用的用户界面来说,ViewGroup作为容器盛装界面中的控件,它可以包含普通的View控件,也可以包 ...

  3. 第2章 Android常见界面布局

    课后习题 1. 列举Android中的常用布局,并简述他们各自的特点 Android中有五种常用布局,分别为RelativeLayout(相对布局).LinearLayout(线性布局).FrameL ...

  4. 《Android移动应用基础教程》(Android Studio)(第二版)黑马教程 课后题答案第2章 Android常见界面布局

    一.填空题 1.ViewGroup 2.LinearLayout 3.TableRow 4.RelativeLayout LinearLayout 5.iInt 二.判断题 1.√ 2.× 3.√ 4 ...

  5. CH2-Android常见界面布局

    文章目录 目标 一.View视图 二.界面布局编写方式 2.1 在XML文件中编写布局 2.2 在Java代码中编写布局 三.界面布局的通用属性 四.线性布局 4.1 LinearLayout 4.2 ...

  6. Android 常见界面控件(ListView、RecyclerView、自定义View篇)

    Android 常见界面控件(ListView.RecyclerView.自定义View篇) 目录 3.3 ListView的使用 3.3.1 ListView控件的简单使用 3.3.2 常用数据适配 ...

  7. Android常见页面布局

    在Android应用程序中,界面由布局和控件组成.布局如框架,控件如砖瓦. View控件 Android所有的UI元素都是通过View控件和ViewGroup容器构建的.在一个应用程序中ViewGro ...

  8. android10桌面布局好看,让你的Android手机界面布局更好看

    机友是否觉得你的Android界面布局不够好看?想换一种手机界面?也许很多朋友都能够很简单Android界面美化,但是太深入的操作就不会了,那么下面我为大家介绍另一种风格的界面,使你的手机界面变的更加 ...

  9. 常见界面布局之LinearLayout线性布局

    1.什么是LinearLayout线性布局 LinearLayout (线性布局)通常指定布局内的子控件水平或者轻直排列.在XML布局文件中定义线性布局的基本语法格式如下: <LinearLay ...

最新文章

  1. Halcon:(3)铝板检测与角点坐标计算
  2. 因缺失log4j.properties 配置文件导致flume无法正常启动。
  3. 精彩回顾 | Dapr闪电说系列
  4. 视频PPT互动问答丨数据库智能运维专题
  5. 数据结构思维 第十一章 `HashMap`
  6. 大数据_Flink_流式处理_简介_流数据处理的应用行业---Flink工作笔记0003
  7. makefile高级应用
  8. 淮阴工学院计算机专业的考研率,淮阴工学院江淮学院(淮阴工学院考研录取率)...
  9. bzoj 3781: 小B的询问(莫队)
  10. Nature命名规范
  11. 云计算概念简述(讲解)
  12. android 视频解决方案,短视频SDK升级为短视频解决方案
  13. 阿里云上的Docker容器镜像仓库
  14. Linux报错 tar: Error Is Not Recoverable: Exiting Now
  15. 组装台式机后进入PE,检测不到硬盘的解决方法
  16. 数据结构2(线性表)
  17. 2018最新APP界面设计教程---手机ui高级实战案例 视频教程(价值320元)
  18. CTFHub_历年真题_MISC——“图片修复”、“磁盘恢复”、“蛛丝马迹”
  19. matlab图形网格线画虚线
  20. 在深圳,报考软考你需要了解这些

热门文章

  1. CSS position定位之static
  2. 彩色图像 psnr matlab,在matlab中的PSNR图像
  3. 01 期货交易所及期货品种
  4. Linux学习之路——常见指令详细总结(中)
  5. Centos 7配置jdk
  6. 红帽linux配置syslog,linux syslog配置
  7. php多维数组删除指定元素,如何删除多维数组中指定元素?
  8. ibm服务器主板电池型号,拆拆党宝刀不老,为十年的IBM R40换主板电池!
  9. 5种方法帮你轻松解决iPhone系统无法更新系统的问题
  10. Elasticsearch教程(6) ES桶聚合Query DSL-Terms Aggregation