• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
常春藤的博客
   首页    新随笔       管理    订阅  订阅

ListView的LayoutParams设置

 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。到底怎么处理,下面的文章再来细述。

posted @ 2015-06-11 11:50  常春藤的博客  阅读(1130)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3