java设置控件的 layout_marginleft 属性

本文详细介绍了在Android应用开发中如何利用LinearLayout和RelativeLayout进行界面布局及样式调整,通过设置LayoutParams参数来实现组件的精确定位与大小控制。
[java]  view plain copy
  1. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  2. lp.setMargins(left, top, right, bottom);  
  3. imageView.setLayoutParams(lp);  

[java]  view plain copy
  1. RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(  
  2.           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  3. rel_btn.height = 60;  
  4. rel_btn.width = 60;  
  5. BtnNext.setLayoutParams(rel_btn);  

[java]  view plain copy
  1. rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);  
  2. rel_btn.leftMargin = 220;  
<?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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/控件1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:textSize="24sp" android:text="控件1" /> <TextView android:id="@+id/控件2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="100dp" android:text="控件2" /> <TextView android:id="@+id/控件3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="控件3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/控件4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:textSize="24sp" android:text="控件4" /> <TextView android:id="@+id/控件5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="控件5" /> <TextView android:id="@+id/控件6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:textSize="24sp" android:text="控件6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/控件7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:textSize="24sp" android:text="控件7" /> <TextView android:id="@+id/控件8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:textSize="24sp" android:text="控件8" /> <TextView android:id="@+id/控件9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:textSize="24sp" android:text="控件9" /> </LinearLayout> </LinearLayout> android:layout_marginleft="10dp"调不了
最新发布
10-18
<!-- 版本信息容器 --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/VersionInfoContainer" android:layout_width="500dp" android:layout_height="100dp" android:layout_marginLeft="350dp" android:layout_marginTop="180dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <!-- 目标版本 --> <TextView android:id="@+id/target_version_text" android:layout_width="0dp" android:layout_height="50dp" android:gravity="center" android:textColor="#FFFF00" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="待传入"/> <!-- 当前版本 --> <TextView android:id="@+id/current_version_text" android:layout_width="0dp" android:layout_height="50dp" android:gravity="center" android:textColor="#FFFF00" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/target_version_text" tools:text="待传入"/> </androidx.constraintlayout.widget.ConstraintLayout> <!-- 结束界面(调整位置避免重叠) --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/updateEnd" android:layout_width="1040dp" android:layout_height="wrap_content" android:layout_marginStart="80dp" android:layout_marginTop="24dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/VersionInfoContainer"> <ImageView android:id="@+id/successIcon" android:layout_width="200dp" android:layout_height="100dp" android:layout_marginLeft="500dp" android:layout_marginTop="320dp" android:visibility="invisible" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <!-- 提示文字 --> <TextView android:id="@+id/updateEndText" android:layout_width="700dp" android:layout_height="50dp" android:layout_marginLeft="250dp" android:layout_marginTop="450dp" android:gravity="center" android:textColor="#FFFF00" android:textSize="28sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="恭喜您,数据更新成功"/> </androidx.constraintlayout.widget.ConstraintLayout> <!--IKONW--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/close" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="460dp" android:layout_marginTop="550dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent"> <!--有数据可提示文言--> <TextView android:id="@+id/closetext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:background="#818181" android:ellipsize="end" android:gravity="center_horizontal|center_vertical" android:hint="@string/search_for_destination" android:maxLength="50" android:singleLine="false" android:text="@string/update_konwed_text" android:textColor="#FFFFFF" android:textSize="28sp" app:layout_constraintStart_toStartOf="@+id/close" app:layout_constraintTop_toTopOf="@+id/close" /> </androidx.constraintlayout.widget.ConstraintLayout> 高度和颜色都异常,版本的高度较为正常按钮高度正常,提示高度十分异常,注意按钮目前的底部即为整个容器的底部
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值