android安卓开发常用布局属性归纳整理

本文详细介绍了Android中的LinearLayout、RelativeLayout、GridLayout、ScrollView、HorizontalScrollView和TableLayout等布局组件的常用属性,包括宽度、高度、方向、对齐方式、权重、边距等,以及ScrollViews的滚动设置和FrameLayout的特殊功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LinearLayout线性布局

常用属性

  • layout_width:布局宽度(match_parent,wrap_conent)
  • layout_height:布局高度(match_parent,wrap_conent)
  • orietation:方向(vertical垂直,horizontal水平)
  • gravity:对齐方式(left, right, center, top, bottom…)
  • background:背景(颜色、图片、选择器)
  • weight:比重(用于瓜分手机屏幕)
  • padding:内边距 (paddingLeft, paddingRight, - paddingTop, paddingBottom)
  • margin:外边距 (marginLeft, marginRight, marginTop, marginBottom)
  • longClickable 设置是否响应长按事件
  • android:minHeight 设置视图最小高度
  • android:minWidth 设置视图最小宽度度
  • nextFocusDown 设置下方 指定视图获得下一个焦点。焦点移动是基于一个在给定方向 查找最近邻居的算法。如果指定视图不存在,移动焦点时将报运行时错误。可以设置imeOptions= actionDone,这样输入完即跳到下一个焦点。
  • nextFocusLeft 设置左边 指定视图获得下一个焦点。
  • nextFocusRight 设置右边 指定视图获得下一个焦点。
  • nextFocusUp 设置上方 指定视图获得下一个焦点。
  • onClick 点击时从上下文中调用指定的方法。这里指定一个方法名称,一般在Activity定义符
    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
    </LinearLayout>

RelativeLayout相对布局

根据父容器常用属性

  • android:layout_alignParentLeft=“true” 父容器左边
  • android:layout_alignParentRight=“true” 父容器右边
  • android:layout_alignParentTop=“true” 父容器顶部
  • android:layout_alignParentBottom=“true” 父容器底部
  • android:layout_centerHorizontal=“true” 水平方向居中
  • android:layout_centerVertical=“true” 垂直方向居中
  • android:layout_centerInParent=“true” 水平垂直都居中

根据兄弟控件常用属性

  • android:layout_toLeftOf=“@+id/button1” 在button1控件左方
  • android:layout_toRightOf=“@+id/button1” 在button1控件右方
  • android:layout_above=“@+id/button1” 在button1控件上方
  • android:layout_below=“@+id/button1” 在button1控件下方
  • android:layout_alignLeft=“@+id/button1” 与button1控件左边平齐
  • android:layout_alignRight=“@+id/button1” 与button1控件右边平齐
  • android:layout_alignTop=“@+id/button1” 与button1控件上边平齐
  • android:layout_alignBottom=“@+id/button1” 与button1控件下边平齐
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </RelativeLayout>

GridLayout网格布局

容器常用属性

  • android:columnCount 最大列数
  • android:rowCount 最大行数
  • android:orientation GridLayout中子元素的布局方向
  • android:alignmentMode alignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值
  • android:columnOrderPreserved 使列边界显示的顺序和列索引的顺序相同,默认是true
  • android:rowOrderPreserved 使行边界显示的顺序和行索引的顺序相同,默认是true
  • android:useDefaultMargins 没有指定视图的布局参数时使用默认的边距,默认值是false

控件元素常用属性

  • android:layout_column 指定该单元格在第几列显示
  • android:layout_row 指定该单元格在第几行显示
  • android:layout_columnSpan 指定该单元格占据的列数
  • android:layout_rowSpan 指定该单元格占据的行数
  • android:layout_gravity 指定该单元格在容器中的位置
  • android:layout_columnWeight (API21加入)列权重
  • android:layout_rowWeight (API21加入) 行权重
    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="3"
        android:orientation="horizontal"
        android:rowCount="3">
    </GridLayout>

ScrollView垂直滚动布局

使用注意

  • layout_width属性值设置为match_parent
  • layout_height属性值设置为wrap_content
  • android:fillViewport属性可以撑满整个页面
  • android:scrollbars 设置滚动条显示。none(隐藏),horizontal(水平),vertical(垂直)。
  • android:scrollbarFadeDuration 设置滚动条淡出效果(从有到慢慢的变淡直至消失)时间,以毫秒为单位。Android2.2中滚动条滚动完之后会消失,再滚动又会出来,在1.5、1.6版本里面会一直显示着。
  • android:scrollbarSize 设置滚动条的宽度。
  • android:scrollbarStyle 设置滚动条的风格和位置。设置值:insideOverlay、insideInset、outsideOverlay、outsideInset
  • android:scrollbarThumbHorizontal 设置水平滚动条的drawable。
  • android:scrollbarThumbVertical 设置垂直滚动条的drawable.
  • android:scrollbarTrackHorizontal设置水平滚动条背景(轨迹)的色drawable
  • android:soundEffectsEnabled 设置点击或触摸时是否有声音效果
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>
</ScrollView>

HorizontalScrollView水平滚动布局

使用注意

  • layout_width属性值设置为wrap_content
  • layout_height属性值设置为match_parent
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    </HorizontalScrollView>

TableLayout 表格布局

容器常用属性

  • android:collapseColumns:设置需要被隐藏的列的序号,从o开始
  • android:stretchColumns:设置允许被拉伸的列的列序号,从o开始
  • android:shrinkColumns:设置允许被收缩的列**的列序号,从o开始

控件元素常用属性

  • android:layout_column:显示在第几列
  • android:layout_span:横向跨几列(占据几列)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:text="第一列"/>
        <Button
            android:layout_width="match_parent"
            android:text="第二列"/>
    </TableRow>

    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:text="第一列"/>
        <Button
            android:layout_width="match_parent"
            android:text="第二列"/>
        <Button
            android:layout_width="match_parent"
            android:text="第三列"/>
        <Button
            android:layout_width="match_parent"
            android:text="第四列"/>
        <Button
            android:layout_width="match_parent"
            android:text="第五列"/>
    </TableRow>

    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:text="第一列"/>
    </TableRow>
</TableLayout>

FrameLayout帧布局

常用属性

  • scrollbars 滚动条 (none、horizontal、vertical)
  • layout_marginTop 上边距
  • layout_marginBottom 下边距
  • layout_marginLeft 左边距
  • layout_marginRight 右边距
  • paddingLeft 左内边距
  • paddingRight 右内边距
  • paddingTop 上内边距
  • paddingBottom 下内边距
  • background 背景(背景色、背景图、背景选择器
  • android:foreground:设置改帧布局容器的前景图像
  • android:foregroundGravity:设置前景图像显示的位置
    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"
            android:background="#FF33ffff" />
        <TextView
            android:layout_width="240dp"
            android:layout_height="240dp"
            android:layout_gravity="center"
            android:background="#FF33ccff" />
        <TextView
            android:layout_width="180dp"
            android:layout_height="180dp"
            android:layout_gravity="center"
            android:background="#FF3399ff" />
        <TextView
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:background="#FF3366ff" />
        <TextView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            android:background="#FF3300ff" />
    </FrameLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WiFiMing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值