2012-01-14 110 views
0

我想出了如何让屏幕顶部的菜单栏。当我移动使用expandableListView创建的主菜单时,它保持原位。不过,我希望菜单栏位于屏幕的底部而不是顶部。我用relativeView玩了一下,然后在底部找到了吧,但是主菜单消失了。 这里是我的主XML文件使用ExpandableListView在屏幕底部放置一个菜单栏

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


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

    <Button 
     android:id="@+id/buttonbefore" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     android:text="back" /> 

    <Button 
     android:id="@+id/buttonnext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="50" 
     android:text="Next" /> 
</LinearLayout> 

<ExpandableListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

<TextView 
    android:id="@+id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="@string/main_no_items" /> 

然后我用这对Java得到它出现在我的计划。

LinearLayout bottomMenu = (LinearLayout)findViewById(R.id.buttons); 

我试图玩弄的严重性,它什么也没做。如果您认为它有帮助,我可以从我的程序中添加更多代码。

回答

2

我假设您的buttons LinearLayout是您所指的“菜单栏”?

您可以使用RelativeLayout或LinearLayout实际完成要查找的内容。

的LinearLayout:

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

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <ExpandableListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

     <TextView 
      android:id="@android:id/empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="@string/main_no_items" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/buttonbefore" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="back" /> 

     <Button 
      android:id="@+id/buttonnext" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Next" /> 
    </LinearLayout> 

</LinearLayout> 

的按钮在底部对齐通过设置重力,而列表被告知给予它的“1”的权重,并设置为占用所有其他的可用空间'0dp'的高度。这将导致它尽可能伸展,而无需将按钮按下屏幕。

的RelativeLayout:

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/buttons" 
     android:orientation="vertical" > 

     <ExpandableListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

     <TextView 
      android:id="@android:id/empty" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="@string/main_no_items" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/buttonbefore" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="back" /> 

     <Button 
      android:id="@+id/buttonnext" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Next" /> 
    </LinearLayout> 

</RelativeLayout> 

的按钮是底端对齐通过设置在父的对准。该列表被告知填充所有高度,但保持在按钮上方。

在一个普遍的说明:为了这个工作,我敢肯定,列表和空视图需要在他们自己的布局组合在一起。

+0

谢谢,我需要添加另一个LinearLayout并将按钮作为第二个LinearLayout – Aaron 2012-01-15 01:13:37

0

将按钮对齐到底部的方法是将顶层LinearLayout更改为RelativeLayout。然后,您可以将属性android:layout_alignParentBottom="true"添加到包含您的两个按钮的LinearLayout @+id/buttons

相关问题