Android 动态设置宽高
程序员文章站
2022-06-16 10:18:26
Android 动态设置宽高 val params: FrameLayout.LayoutParams = view?.layoutParams as FrameLayout.LayoutParams params.width = Utils.dp2pxInt(20) params.height = Utils.dp2pxInt(20) view?.layoutParams = paramsUtils.dp2pxInt 方法: public static int dp2pxInt(...
Android 动态设置宽高
val params: FrameLayout.LayoutParams = view?.layoutParams as FrameLayout.LayoutParams
params.width = Utils.dp2pxInt(20)
params.height = Utils.dp2pxInt(20)
view?.layoutParams = params
Utils.dp2pxInt 方法:
public static int dp2pxInt(Context context, float dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
context.getResources().getDisplayMetrics());
}
宽高也可以设置为子适应的:
params.width = FrameLayout.LayoutParams.MATCH_PARENT
params.height = FrameLayout.LayoutParams.MATCH_PARENT
获取屏幕宽高:
this.resources.displayMetrics.widthPixels
this.resources.displayMetrics.heightPixels
动态设置 margin
:
//4个参数按顺序分别是左上右下
params.setMargins(10,10,10,10)
或
params.leftMargin=10
params.rightMargin=10
params.bottomMargin=10
params.topMargin=10
动态设置 padding
:
view?.setPadding(10,10,10,10)
本文地址:https://blog.csdn.net/lxd_love_lgc/article/details/112900632