Android 布局属性值

padding和margin属性在开发中十分常用,padding意为“填充”,一般用来在控件内部填充布局,而margin意为“边缘”,一般指的是控件外部距父控件的距离,可以结合下面的图片来理解,如图3.9所示。



图3.9 Android 布局示意图

图中序号如表3.1所示。

表3.1 Android布局示意图含义表



3.2.1 Android padding属性用法

下面通过一个实例来看一下这些属性的用法,首先看一下padding属性的用法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#96e25f"
        android:paddingBottom="80dp"
        android:paddingLeft="20dp"
        android:paddingRight="60dp"
        android:paddingTop="40dp"
        android:text="Hello World!" />
</LinearLayout>

为TextView控件添加了四个相关的padding属性,并设置了不同的属性值,为了方便观察还为用来演示的TextView控件添加了背景色(设置了background属性),查看Android Studio的预览窗口即可实时查看效果图,如图3.10所示。


图3.10 Android Padding属性示意图

可以看出,和设置属性值一致,左上右下四个方向的padding值依次变大。

3.2.2 Android margin属性用法

下面看一下margin属性的用法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="30dp"
        android:background="#96e25f"
        android:paddingBottom="80dp"
        android:paddingLeft="20dp"
        android:paddingRight="60dp"
        android:paddingTop="40dp"
        android:text="Hello World!" />
</LinearLayout>

由于LinearLayout中控件默认在左上角显示,所以这里添加了两个margin属性,分别是layout_marginLeft(距左边界的距离)和layout_marginTop(距上边界的距离),效果如图3.11所示。



图3.11 Android Margin属性示意图一

修改代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom|right"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="10dp"
        android:background="#96e25f"
        android:paddingBottom="80dp"
        android:paddingLeft="20dp"
        android:paddingRight="60dp"
        android:paddingTop="40dp"
        android:text="Hello World!" />
</LinearLayout>

为了查看layout_marginBottom(距离底部边界)和layout_marginRight(距离右部边界)的效果,这里为LinearLayout添加了gravity属性并设置其值为bottom|right(控件将置于右下角),再次查看预览窗口,如图3.12所示。



图3.12 Android Margin属性示意图二

可以看出TextView位于右下角,距离其父布局边界底部边界30dp,距离父布局右边边界10dp。

当然除了上面的指定具体“上下左右”边界的值,还提供了padding和layout_margin属性,这时“上下左右”都是相同的值了,下面通过一个实例看一下这两个属性的效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="40dp"
        android:background="#96e25f"
        android:padding="40dp"
        android:text="TextView1" />
</LinearLayout>

为TextView添加了padding属性为40dp,这时候TextView的“上下左右”内间距相同,都为40dp;为TextView添加了layout_margin属性并设置了其值为40dp,这时距左边距40dp,距上边距40dp,效果如图3.13所示。



图3.13 Android Margin属性示意图三

可以看出,此时TextView控件距离模拟器的上边界和左边界的距离都相同了,且TextView里的文字位于TextView正中了。

再次修改下代码为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom|right"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="40dp"
        android:background="#96e25f"
        android:padding="40dp"
        android:text="TextView1" />
</LinearLayout>

为LinearLayout添加了gravity属性其值为bottom|right(右下),这时显示效果如图3.14所示。


图3.14 Android Margin属性示意图四

可以看出,此时TextView距离模拟器底边界和右边界距离相同了。


如果你喜欢作者的文章,还可以购买他的书(京东自营):


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为你介绍一下 Android 线性布局的特点及常用属性Android 线性布局是一种常用的布局方式,可以按照水平或垂直方向排列子视图。它的特点如下: 1. 排列方式简单明了,易于理解和使用。 2. 可以使用 layout_weight 属性来控制子视图在布局中的比例分配。 3. 可以使用 gravity 和 layout_gravity 属性来控制子视图在布局中的对齐方式。 4. 在布局中添加子视图的顺序会影响它们在布局中的位置。 常用的属性包括: 1. android:orientation:设置线性布局的排列方向,可选为 horizontal 和 vertical。 2. android:layout_weight:用于控制子视图在布局中的比例分配,数越大,占用的空间越多。 3. android:gravity:用于控制子视图在布局内的对齐方式,可选为 left、top、right、bottom、center、center_vertical、center_horizontal。 4. android:layout_gravity:用于控制子视图在布局外的对齐方式,可选与 gravity 相同。 5. android:layout_width 和 android:layout_height:用于设置子视图的宽度和高度,可选为 match_parent、wrap_content 或具体数。 需要注意的是,当 LinearLayout 的 orientation 属性设置为 horizontal 时,layout_weight 属性控制的是子视图在布局中的宽度比例分配。 总的来说,线性布局Android 应用程序中常用的布局方式,具有简单易用、方便灵活等特点,掌握其使用方法可以使应用程序的界面更加美观和易于使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值