Android常见的界面结构

打印 上一主题 下一主题

主题 558|帖子 558|积分 1674



目录

​前言
1.线性结构LinearLayout
2.相对结构RelativeLayout
3.表格结构TableLayout
 4.网格结构GridLayout
实现一个盘算器界面
改Button按钮颜色
5.帧结构FrameLayout

前言

在Android应用步伐中,界面是由结构和控件构成的。控件是功能单元,负责接受用户的输入或者展示信息。而结构就是框架,来组织和定位这些控件的位置。
我们先来简朴了解一下Android中一些常见的结构
1.线性结构LinearLayout

线性结构内的子控件通常被指定为程度或者竖直排列。

常用属性
属性名称说明
androidrintation 设置结构内控件的排列顺序
(vertical为竖直,horiztal为程度)
android:layout_weight在结构内设置控件的权重,属性值可以直接写int值
android:layout_width设置结构或控件的宽度
android:layout_height设置结构或控件的高度
android:background设置结构或者控件的配景
android:gravity线性结构中,子容器相对于父容器地点的位置


  • 当layout_width为0时,layout_weight表现程度方向的宽度比例。
  • 当layout_height为0时,layout_weight表现竖直方向的高度比例。
竖直摆放:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <Button
  7.         android:id="@+id/btn1"
  8.         android:text="按钮1"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"/>
  11.     <Button
  12.         android:id="@+id/btn2"
  13.         android:text="按钮1"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"/>
  16.     <Button
  17.         android:id="@+id/btn3"
  18.         android:text="按钮1"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"/>
  21. </LinearLayout>
复制代码
 
我们将父容器的orintation改为horiatal就是程度结构

2.相对结构RelativeLayout

相对结构通过相对定位的方式来指定子控件的位置。

RelativeLayout以父容器或者其他子控件为参照物,来指定结构内子控件的位置。
在相对结构中,其子控件具备一些属性,用于指定子控件的位置,一般是多个一起使用,
相对结构中子控件的属性:

 根据父容器定位

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:layout_margin="40sp">
  6.     <Button
  7.         android:id="@+id/btn1"
  8.         android:text="左上角"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:layout_alignParentLeft="true"/>
  12.     <Button
  13.         android:id="@+id/btn2"
  14.         android:text="上居中"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:layout_centerHorizontal="true"/>
  18.     <Button
  19.         android:id="@+id/btn3"
  20.         android:text="右上角"
  21.         android:layout_width="wrap_content"
  22.         android:layout_height="wrap_content"
  23.         android:layout_alignParentRight="true"/>
  24.     <Button
  25.         android:id="@+id/btn4"
  26.         android:text="左居中"
  27.         android:layout_width="wrap_content"
  28.         android:layout_height="wrap_content"
  29.         android:layout_centerVertical="true"/>
  30.     <Button
  31.         android:id="@+id/btn5"
  32.         android:text="居中"
  33.         android:layout_width="wrap_content"
  34.         android:layout_height="wrap_content"
  35.         android:layout_centerInParent="true"/>
  36.     <Button
  37.         android:id="@+id/btn6"
  38.         android:text="右居中"
  39.         android:layout_width="wrap_content"
  40.         android:layout_height="wrap_content"
  41.         android:layout_alignParentRight="true"
  42.         android:layout_centerVertical="true"/>
  43.     <Button
  44.         android:id="@+id/btn7"
  45.         android:text="左下角"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:layout_alignParentBottom="true"/>
  49.     <Button
  50.         android:id="@+id/btn8"
  51.         android:text="下居中"
  52.         android:layout_width="wrap_content"
  53.         android:layout_height="wrap_content"
  54.         android:layout_centerHorizontal="true"
  55.         android:layout_alignParentBottom="true"/>
  56.     <Button
  57.         android:id="@+id/btn9"
  58.         android:text="按钮9"
  59.         android:layout_width="wrap_content"
  60.         android:layout_height="wrap_content"
  61.       android:layout_alignParentBottom="true"
  62.         android:layout_alignParentRight="true"/>
  63. </RelativeLayout>
复制代码
 
根据子控件来定位

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.     <Button
  6.         android:id="@+id/target_btn"
  7.         android:text="都以我为中心"
  8.         android:textSize="10sp"
  9.         android:textColor="@color/black"
  10.         android:layout_centerInParent="true"
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"/>
  13.     <Button
  14.         android:id="@+id/btn_1"
  15.         android:text="我在中心下面"
  16.         android:layout_width="wrap_content"
  17.         android:layout_height="wrap_content"
  18.         android:textSize="10sp"
  19.         android:textColor="@color/black"
  20.        android:layout_below="@+id/target_btn"
  21.         android:layout_marginTop="40dp"
  22.         android:layout_alignLeft="@+id/target_btn"/>
  23.     <Button
  24.         android:id="@+id/btn_2"
  25.         android:text="我在中心上面"
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content"
  28.         android:textSize="10sp"
  29.         android:textColor="@color/black"
  30.         android:layout_above="@+id/target_btn"
  31.         android:layout_marginBottom="40dp"
  32.         android:layout_alignLeft="@+id/target_btn"/>
  33.     <Button
  34.         android:id="@+id/btn_3"
  35.         android:text="我在中心左面"
  36.         android:layout_width="wrap_content"
  37.         android:layout_height="wrap_content"
  38.         android:textSize="10sp"
  39.         android:textColor="@color/black"
  40.         android:layout_alignBottom="@+id/target_btn"
  41.        />
  42.    
  43.     <Button
  44.         android:id="@+id/btn_4"
  45.         android:text="我在中心右面"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:textSize="10sp"
  49.         android:textColor="@color/black"
  50.         android:layout_alignTop="@+id/target_btn"
  51.         android:layout_toRightOf="@+id/target_btn"
  52.         android:layout_marginLeft="40dp"/>
  53. </Relative
复制代码
 
3.表格结构TableLayout

表格结构采用列、行的情势来管理控件,不需要明确声明此中有多少行和多少列,而是在通过在表格中添加TableRow结构或者控件来控制表格的行数在TableRow结构中添加控件来控制表格的列数。
这种结构和后序讲的GridLayout的区别就是能够订定各列宽度不一样的表格,而网格结构只能订定列宽度一样的结构。
由于TableLayout继承于线性结构LinearLayout结构,以是表格结构支持线性结构的属性,此外,还有其他常用的属性:
属性名称说明
android:stretchColumns设置可拉伸的列。如:android:stretchColumns=“0”,表现第1列可以拉伸
android:shrinkColumns设置可收缩的列。如:android:shrinkColumns=“1,2”,表现第2、3列可以收缩
android:collapseColumns设置可以隐藏的列。如:android:collapseColumns=“0”,表现第1列可以隐藏
此外,表格结构中控件的常用属性
属性名称说明
android:layout_column设置该控件表现的位置,如:android:layout_column="1",表如今第2个位置表现
android:layout_span设置该控件占据第几列,默以为第1列
示例
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent">
  5.    
  6.     <TableRow>
  7.         <Button
  8.             android:text="按钮1"
  9.             android:layout_width="wrap_content"
  10.             android:layout_height="wrap_content"
  11.             android:layout_column="0"/>
  12.         
  13.         <Button
  14.             android:text="按钮2"
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:layout_column="1"/>
  18.     </TableRow>
  19.    
  20.     <TableRow>
  21.         <Button
  22.             android:text="按钮3"
  23.             android:layout_width="wrap_content"
  24.             android:layout_height="wrap_content"
  25.             android:layout_column="1"/>
  26.         <Button
  27.             android:text="按钮4"
  28.             android:layout_weight="1"
  29.             android:layout_width="0dp"
  30.             android:layout_height="wrap_content"
  31.             android:layout_column="1"/>
  32.     </TableRow>
  33.    
  34.     <TableRow>
  35.         <Button
  36.             android:text="按钮5"
  37.             android:layout_weight="1"
  38.             android:layout_width="0dp"
  39.             android:layout_height="wrap_content"
  40.             android:layout_column="2"/>
  41.     </TableRow>
  42. </TableLayout>
复制代码

 
 4.网格结构GridLayout

网格结构是android14.0引入的,使用它可以减少结构嵌套。

常见属性
属性说明
android:columnCount设置最大列数
android:rowCount设置最大行数
androidrientationGridLayout中子元素的结构方向
android:alignmentModealignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值
android:columnOrderPreserved使列边界表现的顺序和列索引的顺序雷同,默认是true
android:rowOrderPreserved使行边界表现的顺序和行索引的顺序雷同,默认是true
android:useDefaultMargins没有指定视图的结构参数时使用默认的边距,默认值是false

 使用layout_columnSpan 、layout_rowSpan时要加上layout_gravity属性,否则没有用果;另外item在边沿时宽高盘算会出现错误,需要我们手动设置宽高,否则达不到想要的效果。
GridLayout在API21时引入了android:layout_columnWeight和android:layout_rowWeight来办理中分问题。
实现一个盘算器界面


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="wrap_content"
  5.     android:columnCount="4"
  6.     android:rowCount="9"
  7.     android:orientation="horizontal">
  8.     <TextView
  9.         android:text="计算器"
  10.         android:textSize="15sp"
  11.         android:layout_columnSpan="4"
  12.         android:layout_gravity="center"/>
  13.     <TextView
  14.         android:id="@+id/result"
  15.         android:layout_gravity="fill"
  16.         android:gravity="end"
  17.         android:layout_columnSpan="4"
  18.         android:text="0"
  19.         android:background="#D5D4CF"
  20.         android:textSize="50dp"/>
  21.     <Button
  22.         android:layout_gravity="fill"
  23.         android:layout_columnWeight="1"
  24.         android:text="%" />
  25.     <Button
  26.         android:layout_gravity="fill"
  27.         android:layout_columnWeight="1"
  28.         android:text="CE" />
  29.     <Button
  30.         android:layout_gravity="fill"
  31.         android:layout_columnWeight="1"
  32.         android:text="C"/>
  33.     <Button
  34.         android:layout_gravity="fill"
  35.         android:layout_columnWeight="1"
  36.         android:text="delete"/>
  37.     <Button
  38.         android:layout_gravity="fill"
  39.         android:layout_columnWeight="1"
  40.         android:text="1/x" />
  41.     <Button
  42.         android:layout_gravity="fill"
  43.         android:layout_columnWeight="1"
  44.         android:text="x^2" />
  45.     <Button
  46.         android:layout_gravity="fill"
  47.         android:layout_columnWeight="1"
  48.         android:text="x^(1/2)"/>
  49.     <Button
  50.         android:layout_gravity="fill"
  51.         android:layout_columnWeight="1"
  52.         android:text="÷"/>
  53.     <Button
  54.         android:layout_gravity="fill"
  55.         android:layout_columnWeight="1"
  56.         android:text="7" />
  57.     <Button
  58.         android:layout_gravity="fill"
  59.         android:layout_columnWeight="1"
  60.         android:text="8" />
  61.     <Button
  62.         android:layout_gravity="fill"
  63.         android:layout_columnWeight="1"
  64.         android:text="9"/>
  65.     <Button
  66.         android:layout_gravity="fill"
  67.         android:layout_columnWeight="1"
  68.         android:text="X"/>
  69.     <Button
  70.         android:layout_gravity="fill"
  71.         android:layout_columnWeight="1"
  72.         android:text="4" />
  73.     <Button
  74.         android:layout_gravity="fill"
  75.         android:layout_columnWeight="1"
  76.         android:text="5" />
  77.     <Button
  78.         android:layout_gravity="fill"
  79.         android:layout_columnWeight="1"
  80.         android:text="6"/>
  81.     <Button
  82.         android:layout_gravity="fill"
  83.         android:layout_columnWeight="1"
  84.         android:text="-"/>
  85.     <Button
  86.         android:layout_gravity="fill"
  87.         android:layout_columnWeight="1"
  88.         android:text="1" />
  89.     <Button
  90.         android:layout_gravity="fill"
  91.         android:layout_columnWeight="1"
  92.         android:text="2" />
  93.     <Button
  94.         android:layout_gravity="fill"
  95.         android:layout_columnWeight="1"
  96.         android:text="3"/>
  97.     <Button
  98.         android:layout_gravity="fill"
  99.         android:layout_columnWeight="1"
  100.         android:text="+"/>
  101.     <Button
  102.         android:layout_gravity="fill"
  103.         android:layout_columnWeight="1"
  104.         android:text="+/-" />
  105.     <Button
  106.         android:layout_gravity="fill"
  107.         android:layout_columnWeight="1"
  108.         android:text="0" />
  109.     <Button
  110.         android:layout_gravity="fill"
  111.         android:layout_columnWeight="1"
  112.         android:text="."/>
  113.     <Button
  114.         android:layout_gravity="fill"
  115.         android:layout_columnWeight="1"
  116.         android:text="="/>
  117. </GridLayout>
复制代码
 
改Button按钮颜色

不过我们实际的盘算器是没有那么显着的绿色的,那怎么改呢?
我们可以找到AndroidManifest.xml,在里找到以下主题。

Ctrl+鼠标左键进入themes.xml文件,将改写为:
  1. Theme.MaterialComponents.DayNight.NoActionBar.Bridge
复制代码
 
当我们返回我们的XML文件就会看到

5.帧结构FrameLayout

帧结构用于在屏幕上创建一块空缺的区域,添加到该区域中的每个子控件占一帧,这些帧会一个一个叠加在一起,后加入的控件会叠加在上一个控件上层。默认环境下,帧结构中的所有控件会与结构的左上角对齐。
2个特殊属性:
属性相关方法说明
android:foregroundsetForeground(Drawable)设置该帧结构容器的前景图像
android:foregroundGravitysetForeground(int)界说绘制前景图像的gravity属性
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent">
  4. <TextView
  5.     android:layout_width="300dp"
  6.     android:layout_height="300dp"
  7.     android:layout_gravity="center"
  8.     android:background="#FF33ffff" />
  9. <TextView
  10.     android:layout_width="240dp"
  11.     android:layout_height="240dp"
  12.     android:layout_gravity="center"
  13.     android:background="#FF33ccff" />
  14. <TextView
  15.     android:layout_width="180dp"
  16.     android:layout_height="180dp"
  17.     android:layout_gravity="center"
  18.     android:background="#FF3399ff" />
  19. <TextView
  20.     android:layout_width="120dp"
  21.     android:layout_height="120dp"
  22.     android:layout_gravity="center"
  23.     android:background="#FF3366ff" />
  24. <TextView
  25.     android:layout_width="60dp"
  26.     android:layout_height="60dp"
  27.     android:layout_gravity="center"
  28.     android:background="#FF3300ff" />
  29. </FrameLayout>
复制代码

总体来讲,FrameLayout由于定位的欠缺,导致它的应用场景也比力少,不过之后使用碎片的时间是可以使用到的。

以上就是在android中几个常见的界面结构。
就先到这里了~
下期预报:android的一些常见的控件。 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表