RelativeLayout内的LinearLayout内部按钮的宽度不相等

时间:2014-06-17 12:19:20

标签: android android-layout

无法理解为什么按钮尺寸不一样。早些时候,我使用LinearLayout而不是RelativeLayout作为根,但是经过一些谷歌搜索我明白,没有机会使用LL制作页脚,所以我把它变成RL并通过使用按钮将前一个LL复制到RL来调整,但是...... {{ 3}}

另外我无法理解,为什么RL中的保证金无论如何都不起作用(我从下面的代码中删除了),以及我如何在RL中保留元素?!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/button_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/all_mentions_Add" />

        <Button
            android:id="@+id/button_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/all_mentions_Delete" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView_mentions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/linearLayout_buttons" >

    </ListView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

只需设置

android:layout_width="0dp"

而不是

android:layout_width="wrap_content"

在你的按钮中,使重量工作(技巧!)

还在LinearLayout中设置android:orientation="horizontal"

答案 1 :(得分:1)

您可以使用LL:

制作页脚
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:orientation="vertical">

    <ListView
        android:id="@+id/listView_mentions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </ListView>

    <LinearLayout
        android:id="@+id/linearLayout_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/all_mentions_Add" />

        <Button
            android:id="@+id/button_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/all_mentions_Delete" />
    </LinearLayout>

</LinearLayout>

如果您想要使按钮的宽度相同,也可以将按钮的宽度更改为match_parent

答案 2 :(得分:1)

Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView_mentions"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>


    <LinearLayout
        android:id="@+id/linearLayout_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/all_mentions_Add" />

        <Button
            android:id="@+id/button_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/all_mentions_Delete" />
    </LinearLayout>
</LinearLayout>