关于layout_weight的困惑

sno_guo 2014-07-23 07:31:03
大家好, android的 linearlayout布局中有一个layout_weight 的选项, 用来分配每个控件占用的比例的, 但我发现一个问题:

我linearlayout中如果只放 Button, TextView这些控件的时候, 是可以按照比例来分配的, 但如果放 ListView 或中间再嵌套一个linearlayout的话, 就好像不起作用了, 请问下为什么, 谢谢.

一下是我嵌套的举例,大家可以测试下.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">

</ListView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
...全文
123 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lionfresh 2014-07-24
  • 打赏
  • 举报
回复
引用 6 楼 sno_guo 的回复:
哦,谢谢两位的回答, 太感谢了, 我把两位的都加上后, 发现可以了, ( width="0dp", 和weight都改成0.5 ) 不明白其中的道理, width="0dp"代表了什么? weight 是比例, 比例为什么要是0.5 ,( 因为我看dedmo上如果平均分配,则都是1:1:1) 还请教两位多多指点, 谢谢.
0dp就是按照比例来自动调整,设置成0.5:0.5和1:1是一样的,只是一个比例关系。
sno_guo 2014-07-24
  • 打赏
  • 举报
回复
哦,谢谢两位的回答, 太感谢了, 我把两位的都加上后, 发现可以了, ( width="0dp", 和weight都改成0.5 ) 不明白其中的道理, width="0dp"代表了什么? weight 是比例, 比例为什么要是0.5 ,( 因为我看dedmo上如果平均分配,则都是1:1:1) 还请教两位多多指点, 谢谢.
sagittarius1988 2014-07-24
  • 打赏
  • 举报
回复
看你是均分高度还是宽度,对应的layout_width或者 android:layout_height 要设置成 0dp
哎,真难 2014-07-24
  • 打赏
  • 举报
回复
你那里需要平分就把那里改成match_parent,如何需要高度平分两个view的android:layout_height改成match_parent
fireyou 2014-07-24
  • 打赏
  • 举报
回复
android:layout_weight="1" 设置这个的时候 android:layout_width这个最好设置为0
张明云 2014-07-23
  • 打赏
  • 举报
回复
你把两个子view的“android:layout_width="wrap_content"”改为“android:layout_width="0dp"”试试。
_Alien 2014-07-23
  • 打赏
  • 举报
回复
两个都是1本身是没什么作用的,你试下一个改成0.8,一个改成0.2

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧