【android】布局之盒模型、对齐方式、填充(类比web理解)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dangbai01_/article/details/82684171

一、盒模型

我买了一盒月饼,月饼盒算是一个父容器,那月饼这个控件,距离月饼盒的边距是多少呢,这里就需要用到margin

margin是指从自身边框到另一个容器边框之间的距离,就是容器外距离。(外边距)

具体到android中的属性:

android:layout_marginLeft="20dp"

android:layout_marginTop="15dp

...

二、对齐方式

相对父控件的对齐方式,是对着左边还是右边还是中间呢

具体到安卓中的属性:

android:layout_gravity=“center"

...

事实证明,只要有父容器,无论是布局还是控件都可以用

三、填充

padding是指自身边框到自身内部另一个容器边框之间的距离,就是容器内距离。(内边距)

比如月饼印了一个花纹,月饼这个控件的花纹距离月饼的边是多少,就是padding了 

具体到安卓中的属性:

android:gravity=“bottom"

四、一个小例子

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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"

    android:orientation="vertical"

    tools:context=".MainActivity">

    <RadioGroup

        android:id="@+id/radioGroup"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        android:layout_gravity="center"

        android:layout_marginTop="150dp"

        >

        <RadioButton

            android:id="@+id/btnMan"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="男"

            android:checked="true"/>

        <RadioButton

            android:id="@+id/btnWoman"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="女"/>

    </RadioGroup>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/dangbai01_/article/details/82684171
今日推荐