andorid常见布局

提示

ctrl+f 可查找关键字来进行快速查找

常见布局代码意思

TableLayout表格布局管理器
GridLayout网格布局管理器
activity_main_xml//设置布局文件
FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”//命名空间
android:background="@drawable/shape"//设置(按钮)的背景
设置id属性就能获取调用布局管理器的配件
xmlns:tools=“http://schemas.android.com/tools”//定义工具命令空间
android:layout_width=“match_parent”//设置布局宽度(与互容器相同)
android:layout_height=“match_parent”//设置布局高度(与互容器相同)亦可设置包括其自身内容(wrap_content)
android:paddingBottom=""//设置内边距的底部
android:paddingLeft=""//设置内边距的左侧
android:paddingRight=""//设置内边距的右侧
android:paddingTop=""//设置内边距的顶部
tools:context=".MainActivity">//指向布局的Activity,不会被打包进apk文件
android:gravity//设置布局管理器各组件的摆放方式
android:ignoreGravity//设置哪个组件不受android:gravity影响
android:id="@+id/user"组件添加id能方便调用,(@id/user)能调用组件
android:layout_above//设置组件参考组件位置, 上方
android:layout_below//设置组件参考组件位置,下方
android:layout_toLeftof//设置组件参考组件位置,左侧
android:layout_toRightof//设置组件参考组件位置,右侧
android:layout_alignParentBottom//设置组件与布局管理器哪边对齐(下方),布尔类型,判断是与否来进行设置
android:layout_alignParentLeft//设置组件与布局管理器哪边对齐(左方),布尔类型,判断是与否来进行设置
android:layout_alignParentRight//设置组件与布局管理器哪边对齐(右边),布尔类型,判断是与否来进行设置
android:layout_alignParentTop//设置组件与布局管理器哪边对齐(上方),布尔类型,判断是与否来进行设置
android:layout_alignBottom//设置组件与哪个组件的下对齐
android:layout_alignLeft//设置组件与哪个组件的左对齐
android:layout_alignRight//设置组件与哪个组件的右对齐
android:layout_alignTop//设置组件与哪个组件的上对齐
android:layout_centerHorizontal//设置组件位于布局管理器的水平居中
android:layout_centerInParent//设置组件位于布局管理器的中间位置
android:layout_centerVertical//设置组件位于布局管理器的垂直居中的位置
RelativeLayout.LayouutParams//设置布局管理器的子组件上,设置在布局管理器上
android:text=“”//定义文本名称
android:background="@mipmap/bg"//能设置图片背景
android:orientation//设置布局管理器内组件排列方式(水平[horizontal]或者垂直[vertical])
android:gravity//设置布局管理器内组件显示的位置,用|分割可以设置多个属性,如right|bottom
android:layout_weight//设置组件所占的权重,设置组件占父容器空间的比例,默认值是0,不设置时,组件有多大就占多大的空间,设置一个大于0的值,每个组件将对父容器剩余空间进行分割,分割大小取决于每个组件所设置的layout_weight属性的值。
EditText//设置编辑框组件
match_parent//设置与父容器相同(与屏幕一样宽),wrap_content//设置包括其自身内容(大小与自身内容有关,自身内容多,就比较大)
android:hint=""//设置提示文本
android:drawableLeft="@mipmap/"//设置图形资源
android:text=""//设置文本
android:textColor="“设置文本颜色
android:background=”“设置背景
android:foreground=“@mipmap/”//为帧布局管理器设置前景图像(始终位于最上层的图像,可看成ps最顶部的图层)
android:foregroundGravity=”"//为帧布局管理器设置前景图像位置
android:layout_gravity=""//可设置放置的位置(居中显示)
android:textSize=""//可设置字体大小
android:paddingTop//设置顶内边距
android:gravity=""//设置对齐方式
android:layout_margin=""//设置外边距
android:layout_marginTop="“设置顶外边距
android:minLines=”"//设置最小行数

布局的经验

1.在布局管理器xml页面上注释的格式<!***xxxx****>


2.</androidx.constraintlayout.widget.ConstraintLayout>在布局管理器中</代表开头 />代表结尾


3.This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints,就在布局管理器xml Design 上点击魔法棒添加约束


4.在左边app-manifests-AndroidMainfest.xml中点击进入可修改主题(android:theme)


5.在左边app-manifests-AndroidMainfest.xml中点击进入android:label=""可修改软件页面题目


6.<TextView>//显示文本信息,继承View

线性布局管理器(LinearLayout)

1.能控制组件纵向或横向进行排列


2.垂直线性布局管理器:android:orientation=“vertical”,每一行只能放置一个组件,而且不能换行


3.水平线性布局管理器:android:orientation:“horizontal”,每一列只能放置一个组件

扫描二维码关注公众号,回复: 10212960 查看本文章

4.使用<LinearLayout>标记

帧布局管理器(FrameLayout):

1.从左上角开始排布,如果重叠,后面的组件会覆盖前面的组件


2.使用<FrameLayout>标记进行添加

表格布局管理器(TableLayout):

  • 1.用<TableRow>添加表格行,每添加一个标记就增加一行,可视为容器,每添加一个组件,就占一列
  • 2.android:collapseColumns=“序号”//可隐藏第几列的内容,从0开始计数,若要隐藏多列则用,进行分割
  • 3.android:stretchColumns=“列的序号”//设置允许被拉伸。可充分适应屏幕大小,同样从0开始计数 若要隐藏多列则用,进行分割
  • 4.android:shrinkColunnms=“列的序号”//设置允许被收缩。当一行内容超出屏幕宽度时,缩小某一列的宽度,从而让某一列显示出来 从0开始计数,若要隐藏多列则用,进行分割
  • 5.添加一个组件就自动添加一行

网格布局管理器(GridLayout)

  • 1.分为行列格,每个单元格放一个组件,一行中超出容器的组件会自动换行
  • 2.用<GridLayout>标记
  • 3.常用的布局代码:
    android:columnCount=""//指定网格的最大列数
    android:orientation=""//当我们为放置其中的组件分配行和列时,水平排列(vertical)或者垂直排列(horizontal)
    android:rowCount=""//指定网格的最大行数
    GridLayout.LayoutParams
    android:layout_column=""//指定子组件位于网格的第几列(从0开始计数)
    android:layout_columnSpan//指定子组件横向跨几列
    android:layout_columnWeight指定子组件在水平方向的权重(此组件分配水平空间的比例)
    android:layout_gravity设置子组件采用什么方式占据这个网格的空间
    android:layout_row="“指定子组件位于网格第几行(从0开始计数)
    android:layout_rowSpan指定子组件纵向换几行
    android:layout_rowWeight指定垂直方向上的权重
    android:layout_gravity=“fill"设置为填充效果
    <ImageView>//设置图片信息
    android:src=”@mipmap/“设置图像视图要显示的图片
    android:layout_marginRight=””//设置右外边距
    android:layout_marginBottom=""//设置底外边距

布局管理器的嵌套

  • 1.布局注意的事项:
    布局管理器的镶嵌规则:1.根布局管理器必须包括xmlns属性,2.在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将它们括起来,3.不能嵌套太深,如果嵌套太深,则会影响性能
发布了7 篇原创文章 · 获赞 0 · 访问量 346

猜你喜欢

转载自blog.csdn.net/u013074761/article/details/105075446