Android --LayoutParams 布局大小属性(在代码中定义组件大小)
LayoutParams 相当于一个 Layout 的信息包,它封装了Layout 的位置、高、宽等信息
- 在代码中设置 组件的大小:LinearLayout.LayoutParams(宽,高);
- 装到组件中的方法:textView.setLayoutParams(xxx);
案例:
public class LayoutParamsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_layout_params); //定义 线性布局 LinearLayout linearLayout = new LinearLayout(this); //布局大小 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); linearLayout.setLayoutParams(layoutParams);
//定义textView TextView textView = new TextView(this); textView.setText("textViewText"); textView.setBackgroundColor(0xffffcc00); //默认 px LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(300, 300); //往 线性布局添加 textView linearLayout.addView(textView,textLayoutParams); // 把线性布局添加到Activity中 setContentView(linearLayout); } }
效果: