简体   繁体   中英

Android align button to bottom right under a listview

I have no idea on how to achieve this layout: 在此输入图像描述 So basically I want a title bar, bellow that I want a listview, while anchored to the bottom I want a button in the right side of the screen.

Right now I am trying with this code (unsuccessfully)

<?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:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView" />
    </LinearLayout>

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

        <ListView
            android:layout_width="match_parent"
            android:layout_height="436dp"
            android:id="@+id/listView"
            android:layout_gravity="right" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:weightSum="1">

        <Button
            android:id="@+id/button_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="ok"
            android:layout_weight="0.45" />
    </LinearLayout>

</LinearLayout>

By using this layout, my listview does not resize when changing from portrait to landscape so my bottom button goes out of screen. Also the bottom button is not aligned to the right side.

Any idea on how to achieve this? So I need 2 "bars" one anchored to the top, and one anchored to the bottom. On the top "bar" I want a TextView, and on the bottom "bar" I want a button aligned to the right. And I want the listView to fill the space between the 2 bars. So there may be empty space in the middle, when listview has just 1-2-3 items (just like in the picture).

Can this be done? Please advise...

Change your top layout to a relatuve layout, then place the button outside your linearlayout and inside your relative layout. Then set gravity layout_gravity to bottom||right

U can also drag t where u want it in the visual part of the xml layout

Change your top layout to a relatuve layout, then place the button outside your linearlayout and inside your relative layout. Then set gravity of the button tolayout_gravity to bottom||right

U can also drag t where u want it in the visual part of the xml layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM