LayoutParams

LayoutParams是View用来告诉其父控件如何放置自己的一种方式。它描述了View希望的宽度和高度,通过不同的ViewGroup.LayoutParams子类实现。核心参数包括MATCH_PARENT和WRAP_CONTENT。

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

LayoutParams是什么

中文翻译过来就是“布局参数”。
LayoutParams是Layout提供给其中的Children使用的,他其实就是告诉布局(Parent)控件(Children)想要多宽,想要多高,看一下官方文档。

1.LayoutParams are used by views to tell their parents how they want to be laid out.
– LayoutParams是View用来告诉它的父控件如何放置自己的。

2.The base LayoutParams class just describes how big the view wants to be for both width and height.
– 基类LayoutParams(也就是ViewGroup.LayoutParams)仅仅描述了这个View想要的宽度和高度。

3.There are subclasses of LayoutParams for different subclasses of ViewGroup.
– 不同ViewGroup的继承类对应着不同的ViewGroup.LayoutParams的子类。

文档第二点:基类LayoutParams也就是(ViewGroup.LayoutParams)所能使用的参数只有两种

//第一种
ViewGroup.LayoutParams.MATCH_PARENT
//第二种
ViewGroup.LayoutParams.WRAP_CONTENT

文档第三点:这里用2张继承图解释一下。
这里写图片描述
这里写图片描述

简单使用

举例TextView

LinearLayout parent = (LinearLayout)findViewById(R.id.ly_parent);
TextView textView = new TextView(this);
textView.setText("举例");
textView.setTextColor(Color.BLUE);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 50);
parent.addView(textView,lp);

https://blog.youkuaiyun.com/yisizhu/article/details/51582622#t1

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值