Android:代码修改layout_marginBottom的属性

本文介绍如何在Android开发中使用Java代码动态修改控件的layout_marginBottom属性,实现UI动态调整,避免重复定义布局文件。

Android开发中,我们经常利用xml来设置控件layout_marginBottom属性的值,那么,如何在java代码中设置这个属性呢?
这样的目的是,实现动态修改UI,而不需要重新定义布局文件。
样例如下:

首先,来看xml的定义(截取一部分):

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="20dp"/>

再来看对应的java代码:

listview= (ListView)findViewById(R.id.listview);
RelativeLayout.LayoutParams layoutParams = (LayoutParams) listview.getLayoutParams();
layoutParams.bottomMargin=0;//将默认的距离底部20dp,改为0,这样底部区域全被listview填满。
listview.setLayoutParams(layoutParams);

这样,就实现了动态修改UI了。

我写了一个布局,我希望due_time_tv很长时,licenseid_tv省略部分内容,如何实现 <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" android:background="@{item.checked ? @drawable/license_checked_background : @drawable/background_optimize_type_unselected}"> <TextView android:id="@+id/tv_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/tpds_all_dp_16" android:textSize="@dimen/tpds_all_text_size_16" app:layout_constraintBottom_toTopOf="@+id/tv_num" android:textColor="@{item.checked ? @color/color_primary : @color/tpds_color_text_color_primary}" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="Unassigned" /> <TextView android:id="@+id/tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:layout_marginBottom="@dimen/tpds_all_dp_16" android:textSize="@dimen/tpds_all_text_size_14" android:textColor="@{item.checked ? @color/color_primary : @color/tpds_color_text_color_primary}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_type" tools:text="10" /> </androidx.constraintlayout.widget.ConstraintLayout>
最新发布
09-04
<?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、付费专栏及课程。

余额充值