转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51331694 文章出自:薛瑄的博客
你也可以查看我的其他同类文章,也会让你有一定的收货!
在java代码中加载view,如何控制view的大小和填充方式呢??
LayoutParams 之控制填充方式
问题:
有个LinearLayout,用来动态加载别的view页面,EditText的宽度是由里面的内容决定 ,无法显示为match_parent的效果
想要的效果:EditText能以match_parent的width来显示。
布局文件:
main 布局:
LinearLayout> LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
test.xml 布局:
LinearLayout> LinearLayout> LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
解决办法:
在动态加载的view时定义一个LayoutParams,在添加view时明确定义这个view的LayoutParams
public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout layout = (LinearLayout) findViewById(R.id.layout); View view = getLayoutInflater().inflate(R.layout.test, null); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); layout.addView(view, params); } }1
2
3
4
5
6
7
8
9
10
11
12
13
LayoutParams 之控制宽度和高度
private Button mbtn; mbtn = (Button) findViewById(R.id.btn_test); LayoutParams lp; lp=mbtn.getLayoutParams(); lp.width=100; lp.height=200; mbtn.setLayoutParams(lp);1
2
3
4
5
6
7
http://blog.csdn.net/darlk/article/details/7578020
http://www.eoeandroid.com/thread-5687-1-1.html
关注我的公众号,轻松了解和学习更多技术