Android XML 布局基础(四)内外边距(margin、padding)

本文详细讲解了Android布局中的外边距layout_margin和内边距padding概念,包括各自属性如marginTop、marginBottom等的含义,并通过实例展示了如何正确设置它们。同时强调了两者的一次性设置限制。

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

  • 内外边距属性

    外边距内边距
    layout_margin外边距padding内边距
    layout_marginTop上边距paddingTop上内边距
    layout_marginBottom下边距paddingBottom下内边距
    layout_marginLeft左边距paddingLeft左内边距
    layout_marginRight右边距paddingRight右内边距

    注意:layout_marginpadding 不能像 css 中可以填写一次性填写四个方位的间距值,只能填写单个间距值,也就是四周统一用一个值。

    android:padding="40dp 40dp 40dp 40dp" ×
    
    android:padding="40dp"                √
    
  • 布局案例

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <!-- 由于最外层不是 Layout,则这里为根布局 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffc">
            <!-- 再添加一个内部视图布局 -->
            <LinearLayout
                android:layout_marginTop="200dp"
                android:layout_marginLeft="70dp"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:padding="40dp"
                android:background="#fcf">
                <!-- 添加视图组件 -->
               <View
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:background="#cff"/>
            </LinearLayout>
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卡尔特斯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值