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

1,320 阅读1分钟
  • 内外边距属性

    外边距内边距
    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