xml有两个按钮,但弹出对话框只有一个

时间:2013-06-29 00:03:16

标签: android android-linearlayout android-xml

我的弹出对话框xml是:

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/tastePickTitle"
        android:text="Select a Taste: "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/taste_array"

         />

    <View
        android:layout_width="fill_parent"
        android:layout_height="30dp">
    </View>

    <TextView
        android:id="@+id/ammountPickTitle"
        android:text="How much taste: "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/ammount_array"

         />

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

     <Button
        android:id="@+id/addTasteButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add"
        android:onClick="addTasteNow" />

     <Button
        android:id="@+id/cancelButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:onClick="cancelTaste" />


     </LinearLayout>




</LinearLayout>

但是当我看到弹出对话框时,它显示如下:

enter image description here

我希望两个按钮彼此相邻。

2 个答案:

答案 0 :(得分:1)

您的第一个按钮有android:layout_width="fill_parent"。它应该是wrap_content或使用weight s。您的第一个Button占用了所有可用空间。如果他们是wrap_content那么他们将占用他们需要的空间。如果您希望它们拥有均匀的空间,那么您可以执行类似操作,例如为LinearLayout父级提供weightSum为2,并为每个父级提供weight 1。当您使用{{ 1}}为weight设置layout_width为0dp,为horizontal orientation设置height为0 dp

因此vertical orientation的{​​{1}}可能看起来像

LinearLayout

答案 1 :(得分:0)

为什么不尝试在线性布局中添加按钮? 在任何情况下它也应该与你的代码一起工作......我很想知道它为什么不... 我很抱歉我的英语,但我希望我能帮到你。 这是我的解决方案(在LinearLayout中插入两个按钮):

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/addTasteButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addTasteNow"
        android:text="Add" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/cancelButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="cancelTaste"
        android:text="Cancel" />
</LinearLayout>