Android 布局属性


注意本文为布局的属性而非控件属性,这里的布局指的是LinearLayout、RelativeLayout等。

常用属性

android:id

为该布局指定id,非必须,作为父布局的子布局时可能会用上,如在RecyclerView中。

android:orientation

该属性指定在当前布局下控件的排列方向

  • "vertical"表示竖直方式对齐;
  • "horizontal"表示水平方式对齐。

android:layout_width

布局宽度。

  • “match_parent”:匹配屏幕(匹配父一级,对控件来说是匹配布局),即整个屏幕宽度;
  • “wrap_content”:包裹住内容,内容有多大,宽度就会相应的自动调整为多大;
  • “1dp”、“2dp”……

android:layout_height

布局高度。

  • “match_parent”:匹配屏幕(匹配父一级,对控件来说是匹配布局),即整个屏幕高度;
  • “wrap_content”:包裹住内容,内容有多大,高度就会相应的自动调整为多大;
  • “1dp”、“2dp”……

android:background

指定背景颜色,取值可以为"@color/black"、“@color/white”,还可以直接取十六进制颜色码,如黑色为"#000000"、大红为"#ff0000"。

android:gravity

指定其子类布局或控件在该布局或控件中的显示位置,是对子控件的设置。如指定控件在布局内部的显示位置,指定文本在文本框中的显示位置。
取值可以是"left"、“right”、“top”、“bottom”、“start”、“end”、“center”、“center_horizon”、“center_vertical”。

margin类属性

以下属性一般不会用于布局,较常用于控件。

android:layout_margin

指定当前布局的四条边界与父布局(或屏幕)边缘的距离。取值可以为 “1dp”、“2dp”……
类似的,layout_marginLeft、layout_marginRight、layout_marginTop、layout_margingBottom 类似。

android:layout_marginLeft

指定当前布局的左边界与父布局(或屏幕)左边缘的距离。取值可以为 “1dp”、“2dp”……

android:layout_marginRight

指定当前布局的右边界与父布局(或屏幕)右边缘的距离。取值可以为 “1dp”、“2dp”……

android:layout_marginTop

指定当前布局的上边界与父布局(或屏幕)上边缘的距离。取值可以为 “1dp”、“2dp”……

android:layout_marginBottom

指定当前布局的下边界与父布局(或屏幕)下边缘的距离。取值可以为 “1dp”、“2dp”……

android:layout_marginStart

关于android:layout_marginStart、layout_marginEnd,涉及到从右到左(RTL,Right-to-Left)UI布局,该布局方式默认是关闭的,即默认为从左到右(LTR,从左到右,Left-to-Right)布局。若要开启RTL布局,则将android:supportsRtl 设置为 “true” 。
android:layout_marginStart:如果在LTR布局模式下,该属性等同于android:layout_marginLeft。如果在RTL布局模式下,该属性等同于android:layout_marginRight。可简单理解为与前一个同级布局或控件的距离。
参考:https://blog.csdn.net/qq_43290288/article/details/99216528

android:layout_marginEnd

关于android:layout_marginStart、layout_marginEnd,涉及到从右到左(RTL,Right-to-Left)UI布局,该布局方式默认是关闭的,即默认为从左到右(LTR,从左到右,Left-to-Right)布局。若要开启RTL布局,则将android:supportsRtl 设置为 “true” 。
android:layout_marginEnd:如果在LTR布局模式下,该属性等同于android:layout_marginRight。如果在RTL布局模式下,该属性等同于android:layout_marginLeft。可简单理解为与后一个同级布局或控件的距离。
参考:https://blog.csdn.net/qq_43290288/article/details/99216528

android:layout_marginHorizontal

定义水平方向上的两个边界距离,即layout_marginHorizontal相当于同时定义 layout_marginLeft 和 layout_marginRight。

android:layout_marginVertical

定义垂直方向上的两个边界距离,即layout_marginVertical相当于同时定义 layout_marginTop 和 layout_marginBottom。



参考:
Android 第一行代码(第2版)
https://blog.csdn.net/huweiliyi/article/details/122909530
https://blog.csdn.net/qq_43290288/article/details/99216528
https://blog.csdn.net/qq_37492806/article/details/84660999

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值