android中 android:gravity 和 layout_gravity的区别

本文详细解释了在Android开发中,如何利用gravity和layout_gravity属性实现组件在Relativelayout和Linearlayout中的精确定位。通过垂直、水平和居中对齐的实例,深入探讨了这两个属性在不同场景下的使用方法,为开发者提供了清晰的指导。

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

无论在relativelayout还是linearlayout中gravity和layout_gravity都有效果

gravity是指对象中的对象相对于自己的位置。

layout_gravity 是该对象相对于父对象的位置。

一共中居中,垂直居中,水平居中三种情况

<?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/wallpaper1" android:orientation="vertical" tools:context=".activity.MainActivity"> <Button android:id="@+id/bt_start_float" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:text="打开悬浮窗"> </Button> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="15dp" android:layout_weight="0.5" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="12dp" android:paddingTop="6dp" android:paddingRight="12dp" android:paddingBottom="6dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_logo" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="导播系统" android:textColor="@color/text_color" android:textSize="@dimen/text_16sp" android:textStyle="bold" /> <View android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:id="@+id/ic_zhuti" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_zhuti" /> <ImageView android:id="@+id/ic_shumingshuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="@mipmap/ic_shumingshuo" /> <ImageView android:id="@+id/ic_tuichu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="@mipmap/ic_tuichu" /> <ImageView android:id="@+id/ic_shezhi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:background="@mipmap/ic_shezhi" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_weight="1" android:orientation="horizontal"> <LinearLayout android:id="@+id/start_living" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@mipmap/bg_ground11" android:gravity="center_vertical" android:orientation="vertical" android:padding="12dp"> </LinearLayout> <LinearLayout android:id="@+id/btn_shortcut" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:background="@mipmap/bg_ground111" android:gravity="center_vertical" android:orientation="vertical" android:padding="12dp"> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_margin="12dp" android:layout_weight="5.1" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextureView android:id="@+id/texture" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" android:visibility="gone" /> <com.cw.liveguidestation.view.layer.MultiLayerEditor android:id="@+id/layerEditor" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:id="@+id/ll_sound" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginStart="10dp" android:layout_weight="1" android:orientation="vertical" android:visibility="visible"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/nav_rlv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/bg_nav" android:orientation="horizontal" /> <FrameLayout android:id="@+id/fragment_container1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/ll_video" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginStart="10dp" android:layout_weight="1" android:orientation="vertical" android:visibility="gone"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/bg_de" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/jiweiChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一号机位" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@mipmap/ic_xiala1" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@mipmap/btn_ok" android:gravity="center" android:paddingLeft="5dp" android:paddingTop="2dp" android:paddingRight="5dp" android:paddingBottom="2dp" android:text="确认" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:background="@mipmap/btn_esc" android:gravity="center" android:paddingLeft="5dp" android:paddingTop="2dp" android:paddingRight="5dp" android:paddingBottom="2dp" android:text="停止" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="背景画面" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_h" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@mipmap/ic_v" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/llImageBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="图片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivImageBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llCameraBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="摄像头" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivCameraBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llVideoBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="视频" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivVideoBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llSlideBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="幻灯片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivSlideBg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" android:visibility="invisible" /> </LinearLayout> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvContentBg" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- <ImageView--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginTop="10dp"--> <!-- android:background="@mipmap/ic_add" />--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="主播画面" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <androidx.appcompat.widget.SwitchCompat android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" app:thumbTint="@color/text_color" app:trackTint="@color/switch_color" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_h" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@mipmap/ic_v" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="摄像头" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="数字人" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:background="@mipmap/sel_l" /> </LinearLayout> </LinearLayout> <!-- <androidx.recyclerview.widget.RecyclerView--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="wrap_content"/>--> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@mipmap/ic_add" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="扣绿幕" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.SwitchCompat android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" app:thumbTint="@color/text_color" app:trackTint="@color/switch_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="色相值" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平滑度" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="相似度" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿校对" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/100" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="前景画面" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <androidx.appcompat.widget.SwitchCompat android:id="@+id/switchFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" app:thumbTint="@color/text_color" app:trackTint="@color/switch_color" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_h" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@mipmap/ic_v" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/llIMageFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="图片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivImageFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llCameraFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="摄像头" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivCameraFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llVideoFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="视频" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivVideoFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:id="@+id/llSlideFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="幻灯片" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <ImageView android:id="@+id/ivSlideFg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:visibility="invisible" android:background="@mipmap/sel_l" /> </LinearLayout> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvContentFg" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- <ImageView--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginTop="10dp"--> <!-- android:background="@mipmap/ic_add" />--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:gravity="center" android:text="音频输入" android:textColor="@color/text_color" android:textSize="@dimen/text_12sp" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> <!-- <androidx.recyclerview.widget.RecyclerView--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="wrap_content"/>--> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@mipmap/ic_add" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="请对着音频输入设备说话,检测声音" android:textColor="@color/text_color" android:textSize="@dimen/text_10sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_bg" /> <androidx.appcompat.widget.AppCompatSeekBar style="@style/CustomVerticalSeekBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="1" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout> </LinearLayout> <SeekBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="0" android:visibility="gone" app:layout_constraintBottom_toTopOf="@+id/tvvv" tools:ignore="MissingConstraints" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_weight="3.4" android:background="@mipmap/bg_ground2" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/nav_rlv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal" /> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout> 这个布局中的layerEditor添加全屏非全屏效果
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值