android:layout_gravity与android:gravity

本文详细介绍了Android布局管理器LinearLayout的使用方法,包括如何设置控件的宽高、对齐方式、方向以及如何利用layout_gravity属性来精确控制控件在父布局中的位置。通过实例演示了不同属性的应用效果,帮助开发者更好地掌握Android UI布局的灵活性。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text"
        />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <Button 
            android:id="@+id/ture_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"/>
        <Button 
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"/>
    </LinearLayout>

</LinearLayout>

gravity
android:gravity 用来设置该控件里的子控件的位置。如图。
如果在Button控件里设置这个属性 就会控制Button控件里的文字位置,
android:layout_gravity 用来设置该控件在其父控件里的位置。
如果在Button控件里设置这个属性 就会控制Button控件在LinearLayout中的位置。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text"
        />
    <LinearLayout
        android:layout_width="match_parent"//将宽度匹配父类
        android:layout_height="wrap_content"
        android:orientation="vertical">//设为垂直排列
        <Button 
            android:id="@+id/ture_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="@string/true_button"/>
        <Button 
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/false_button"/>
    </LinearLayout>

</LinearLayout>

layout_gravity
由于是线性布局 所以设第二个LinearLayout为垂直排列 才能看出效果

线性布局将它所包含的控件在线性方向上依次排列。
如果为水平排列(horizontal),一个控件就会占一列,一列一列一次排下去,并且内部的控件绝不能将宽度设为match_parent。
如果为垂直排列(vertical),一个控件就会占一行,并且内部的控件不能将宽度设为match_parent。
而控件的layout_gravity属性在LinearLayout布局中,只能设置在它占的那一列(或者那一行)的位置。如图。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值