代码里面修改layout_gravity

本文主要探讨如何在代码中修改RelativeLayout的layout_gravity属性,以实现界面元素的定位调整,适用于Android开发人员优化UI布局。

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

public void change(View view) {
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mImageVew.getLayoutParams();
    layoutParams.gravity = Gravity.CENTER_VERTICAL;
    mImageVew.setLayoutParams(layoutParams);
}

 

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        android:onClick="change"
        android:layout_gravity="center_horizontal"
        />

</LinearLayout>

RelativeLayout

 

public void change(View view) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageVew.getLayoutParams();
        layoutParams.removeRule(RelativeLayout.CENTER_VERTICAL);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mImageVew.setLayoutParams(layoutParams);
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        android:onClick="change"
        />

</RelativeLayout>

 

<?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" android:padding="20dp" android:gravity="center"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/logo" android:layout_gravity="center_horizontal" android:layout_marginBottom="30dp"/> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" android:inputType="text" android:maxLines="1"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:inputType="textPassword" android:maxLines="1"/> </com.google.android.material.textfield.TextInputLayout> <CheckBox android:id="@+id/cb_remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:layout_gravity="start" android:layout_marginBottom="10dp"/> <CheckBox android:id="@+id/cb_auto_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" android:layout_gravity="start" android:layout_marginBottom="20dp"/> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:backgroundTint="@color/purple_500" android:textColor="@color/white"/> <ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" android:layout_marginTop="20dp"/> </LinearLayout> 美化
06-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值