android:layout_weight在线性布局中不能平分

时间:2015-01-27 20:28:37

标签: android android-layout layout android-linearlayout android-layout-weight

基本上我有2个线性布局嵌套在一个垂直线性布局中。每个子线性布局中都有一个按钮。我希望2个孩子的线性布局在页面上垂直分割,因此我将每个重量值设置为' 1'。我知道我可以把按钮放在父线性布局中,但第一个线性布局是水平的,因为我想在它旁边放另一个按钮,这样顶部有2个按钮,底部有一个按钮,我希望顶部按钮和底部按钮的高度将页面高度分成两半。然而,这是我的结果 得到

The top button is clearly taking up more of the height

顶部按钮显然占据了更多的高度,我不明白为什么

这是我的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>    

4 个答案:

答案 0 :(得分:0)

在主要的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>

答案 1 :(得分:0)

尝试将根布局的layout_height更改为match_parrent。

答案 2 :(得分:0)

尝试改变这样,

<?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 :(得分:0)

##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>
相关问题