如何通过代码设置TextView的Margin参数

2025-01-26 09:47:26
推荐回答(1个)
回答1:

首先定义个LayoutParams,然后在设置margin,在把这个LayoutParams设置给控件,如下:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);//4个参数按顺序分别是左上右下

mView.setLayoutParams(layoutParams);