转载的关于LayoutParams
//创建一个线性布局 |
02 |
private LinearLayout mLayout; |
03 |
mLayout = (LinearLayout) findViewById(R.id.layout); |
04 |
//现在我要往mLayout里边添加一个TextView |
05 |
//你可能会想直接在布局文件里边配置不就O 了 那是 但是这里为了说明问题我们用代码实现 |
06 |
TextView textView = new TextView(Activity01. this ); |
07 |
textView.setText( "Text View " ); |
08 |
//这里请不要困惑这里是设置 这个textView的布局 FILL_PARENT WRAP_CONTENT 和在xml文件里边设置是一样的如 |
09 |
/**<TextView |
10 |
android:layout_width="fill_parent" |
11 |
android:layout_height="wrap_content" |
12 |
android:text="Text View"/>*/ |
13 |
//在xml里边怎么配置高宽大家都会的。 |
14 |
//第一个参数为宽的设置,第二个参数为高的设置。 |
15 |
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( |
16 |
LinearLayout.LayoutParams.FILL_PARENT, |
17 |
LinearLayout.LayoutParams.WRAP_CONTENT |
18 |
); |
19 |
//调用addView()方法增加一个TextView到线性布局中 |
20 |
mLayout.addView(textView, p); |
21 |
//比较简单的一个例子 |