【问题标题】:android:layout_weight in linear layouts not dividing equallyandroid:layout_weight 在线性布局中不均分
【发布时间】:2015-01-27 20:28:36
【问题描述】:
基本上我有 2 个线性布局嵌套在一个垂直线性布局中。每个子线性布局中都有一个按钮。我希望 2 个子线性布局在页面上垂直平均划分,因此我将它们的每个权重值设置为“1”。我知道我可以将按钮放在父线性布局中,但第一个线性布局是水平的,因为我想在它旁边放置另一个按钮,以便顶部有 2 个按钮,底部有一个按钮,我想要顶部按钮和底部按钮的高度将页面的高度分成两半。然而这是我的结果
得到
顶部按钮显然占据了更多的高度,我不明白为什么
这是我的 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:padding="15dp"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="0dp" >
<Button
android:id="@+id/buttonOffence"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_offence" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="0dp" >
<Button
android:id="@+id/buttonCoin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_coin" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
标签:
android
android-layout
layout
android-linearlayout
android-layout-weight
【解决方案1】:
尝试将根布局的 layout_height 更改为 match_parrent。
【解决方案2】:
试着这样改变,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/holo_red_light"
android:orientation="vertical"
android:padding="15dp"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/buttonCoin"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
【解决方案3】:
在主 LinearLayout 中,只需将值从 wrap_content 更改为 match_parent,就可以解决问题(对于 layout_width 和 layout_height)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical"
android:padding="15dp"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/buttonOffence"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/buttonCoin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
</LinearLayout>
【解决方案4】:
##main.xml##
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="vertical"
android:padding="15dp"
android:weightSum="2" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/buttonOffence"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="OFFENCE"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<Button
android:id="@+id/buttonCoin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="JUST FLIP A COIN"
/>
</LinearLayout>
</LinearLayout>