2011-09-30 113 views
2

我不太擅长创建android布局,所以我无法将按钮对齐到MainView的底部。底部的按钮

图片:

enter image description here

布局:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> 


    <ListView android:id="@+id/lv_pizza" android:layout_width="fill_parent" 
       android:layout_height="wrap_content" android:layout_weight="1"></ListView> 
    <Button android:layout_width="fill_parent" android:id="@+id/bt_add" 
      android:layout_height="wrap_content" android:text="hinzufügen"></Button> 

    <RelativeLayout android:layout_width="fill_parent" android:id="@+id/relativeLayout2" android:layout_height="fill_parent"> 
    </RelativeLayout> 
    </LinearLayout> 

请帮

+0

您是否需要realtiveLayout?或者把它用于解决问题? – Ronnie

回答

4

这适用于线性布局,注意就行了layout_weight1 - 这是什么推按钮到底部:

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

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

    <Button android:id="@+id/bt_add" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="hinzufügen" /> 

</LinearLayout> 

然后,这在UI编辑器中看起来像这样:

enter image description here

+2

此外,请勿在[ListView]上设置['layout_height =“wrap_content”](http://www.youtube.com/watch?v=wDBM6wVEO70&feature=player_detailpage#t=2456s) –

+0

...和[永远不要将'ListView'放入'ScrollView'](http://www.youtube.com/watch?v=wDBM6wVEO70#t=2562s)。 –

0

使用的的LinearLayout内部组件的layout_weight。如果ListView的权重为1.0,它应该将按钮推到底部。我用ScrollLayout完成了这个工作,它运行良好,允许滚动布局内的组件实际滚动。你可以把你的ListView放到ScrollLayout中,让它增长到可见的尺寸之外。

此外,第二个相关布局也不是必须的,因为它不会添加任何内容。

+0

[不要把'ListView'放入'ScrollView'](http://www.youtube.com/watch?v=wDBM6wVEO70#t=2562s) - 它会弄得一团糟。 –

0

您可以在按钮中设置marginTop直到底部或将布局更改为相对并设置位置。

2

嗨,你可以这样设置

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


<ListView android:id="@+id/lv_pizza" android:layout_width="fill_parent" 
android:layout_above="@+id/bt_add" android:layout_height="fill_parent" ></ListView> 

<Button android:layout_width="fill_parent" android:id="@+id/bt_add" 
android:layout_gravity="bottom" android:layout_height="wrap_content" android:text="hinzufügen"></Button> 


</RelativeLayout> 
+0

这不会导致所需的布局,除非您用'android:layout_alignParentBottom =“true”'替换'android:layout_gravity =“bottom”'。否则是'LinearLayout'的一个不错的选择。 –

1

卸下RelativeLayout将推动按钮,屏幕下方...