【问题标题】:Putting a menu bar on the bottom of the screen while using ExpandableListView使用 ExpandableListView 时将菜单栏放在屏幕底部
【发布时间】:2012-01-14 23:14:30
【问题描述】:

我想出了如何将菜单栏放在屏幕顶部。当我在使用 expandableListView 创建的主菜单中移动时,它会保持原位。但是,我希望菜单栏位于屏幕底部而不是顶部。我玩弄了 relativeView 并得到了底部的栏,但是,主菜单消失了。 这是我的主要 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: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);

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

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

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

    您实际上可以使用RelativeLayout 或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”来告知列表占用所有其他可用空间。这将使它尽可能地伸展,而无需将按钮按到屏幕外。

    相对布局:

    <?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>
    

    通过在父级上设置对齐方式,按钮底部对齐。该列表被告知填充所有高度,但保持在按钮“上方”。

    一般来说:为了让它工作,我很确定列表和空视图需要在它们自己的布局中组合在一起。

    【讨论】:

    • 谢谢,我需要添加另一个 LinearLayout 并将按钮作为第二个 LinearLayout
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2016-03-27
      • 2013-10-13
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多