LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
// 配置布局属性
params.leftMargin = 30; // 左边距
params.topMargin = 30; // 上边距
params.width = 100; // 宽
params.height = 200; // 高
// 将配置好的布局参数设置到视图中
tv.setLayoutParams(params);
// 将视图模块添加到布局模块中
layout.addView(tv);
-
一般而言,建议不要使用绝对单位(如像素
px
)来指定布局宽度和高度,最好是使用相对测量单位(如与密度无关的像素单位dp
、wrap_content
、match_parent
),有利于确保应用在各类尺寸的设备屏幕上正确显示。 -
wrap_content
:视图将其大小调整为内容所需要的尺寸。 -
match_parent
:视图尽可能采用其父视图组所允许的最大尺寸。
xml
布局代码
<androidx.constraintlayout.widget.ConstraintLayout 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”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“#ffc”>
<LinearLayout
android:layout_marginTop=“200dp”
android:layout_marginLeft=“70dp”
android:layout_width=“300dp”
android:layout_height=“300dp”
android:background=“#fcf”>
<TextView
android:id=“@+id/dzm”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginLeft=“100dp”
android:layout_marginTop=“200dp”
android:text=“DZM TEST”
android:textSize=“20sp” />
新的开始
改变人生,没有什么捷径可言,这条路需要自己亲自去走一走,只有深入思考,不断反思总结,保持学习的热情,一步一步构建自己完整的知识体系,才是最终的制胜之道,也是程序员应该承担的使命。
《系列学习视频》
《系列学习文档》
《我的大厂面试之旅》
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
ttps://bbs.csdn.net/topics/618156601)**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!