首页 > 其他 > 详细

ListView的LayoutParams设置

时间:2015-06-11 12:33:15      阅读:119      评论:0      收藏:0      [点我收藏+]
 1                     // Temp is the root view that was found in the xml
 2                     final View temp = createViewFromTag(root, name, attrs, false);
 3 
 4                     ViewGroup.LayoutParams params = null;
 5 
 6                     if (root != null) {
 7                         if (DEBUG) {
 8                             System.out.println("Creating params from root: " +
 9                                     root);
10                         }
11                         // Create layout params that match root, if supplied
12                         params = root.generateLayoutParams(attrs);
13                         if (!attachToRoot) {
14                             // Set the layout params for temp if we are not
15                             // attaching. (If we are, we use addView, below)
16                             temp.setLayoutParams(params);
17                         }
18                     }

从上面可以看到,如果在inflate xml文件时,如果没有指定parent的时候,layout_width是不会生效的。

 1 mInflater.inflate(R.layout.apps_listview_item, parent, false); 

<com.helen.launcher4.TestLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:orientation="horizontal"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">

上面的160dp,如果没有指定parent,是不会生效。

当然这里的parent其实就只有一个目的,使layoutparam生效,其他毫无目的,所以随便指定一个view即可,这里android设计的不太好。

 

如果没有指定parent,layoutparam默认值是什么呢?

Listview是设计成一个可以上下滚动的viewgroup,所以width是占ListView所有的宽度,即MeasureSpec.EXACTLY

height呢?高度是MeasureSpec.UNSPECIFIED, size = 0.

也就是说,高度是随意定的。这会带来一个问题,当子节点使用match_parent的时候,子节点并不知道parent有多少高度,所以子节点的MeasureSpec会是UNSPECIFIED。

GridView在处理UNSPECIFIED的时候,很奇怪,并不是wrap_content。到底怎么处理,下面的文章再来细述。

ListView的LayoutParams设置

原文:http://www.cnblogs.com/zyfa/p/4568668.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!