LayoutParams

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.csdn.net/yisizhu/article/details/51582622#t1

猜你喜欢

转载自blog.csdn.net/weixin_37418246/article/details/82498364