[英]Putting a menu bar on the bottom of the screen while using ExpandableListView
我想出了如何在屏幕頂部顯示菜單欄。 當我在使用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);
我試着用重力來玩,它沒有做任何事情。 如果您認為它有用,我可以從我的程序中添加更多代碼。
我假設您的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>
通過在父級上設置對齊,按鈕是底部對齊的。 該列表被告知填充所有高度,但保持在按鈕上方。
總的來說:為了使它能夠工作,我很確定列表和空視圖需要在它們自己的布局中組合在一起。
將按鈕對齊到底部的方法是將頂級LinearLayout更改為RelativeLayout。 然后你可以將屬性android:layout_alignParentBottom="true"
到你的LinearLayout @+id/buttons
,它們包含你的兩個按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.