Android: jak można wyrównać przycisk na dole i listview powyżej?

Chcę mieć przycisk na dole listview.

Jeśli używam relativeLayout/FrameLayout, wyrównuje się, ale listView schodzi do bardzo bottona.

(Za przyciskiem na dole)

Tutaj wpisz opis obrazka

FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </FrameLayout>
</FrameLayout>

RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </RelativeLayout>
</RelativeLayout>

Powyżej dwa kody działają tylko jak na pierwszym obrazku. Chcę drugiego obrazu.

Czy ktoś może pomóc? Dziękuję.
Author: jclova, 2011-02-08

8 answers

Celem FrameLayoutjest nakładanie rzeczy na siebie. Nie tego chcesz.

W twoim przykładzie RelativeLayout ustawisz ListView S wysokość i szerokość na MATCH_PARENT to sprawi, że zajmie ona tyle samo miejsca co jego rodzic, a tym samym zajmie całą przestrzeń na stronie (i obejmuje przycisk).

Spróbuj czegoś takiego:

<LinearLayout 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical">
  <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"/>
  <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/>
</LinearLayout>

layout_weight określa sposób wykorzystania dodatkowej przestrzeni. Button nie chce rozciągać się poza wymaganą przestrzeń, więc ma masę 0. ListView chce zająć całą dodatkową przestrzeń, więc ma wagę 1.

Możesz osiągnąć coś podobnego używając RelativeLayout, ale jeśli są to tylko te dwa elementy, to myślę, że LinearLayout jest prostsze.

 183
Author: Cheryl Simon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-05 11:20:10
  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#ffffff"
  >

    <ListView android:id="@+id/ListView01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1">
    </ListView>

    <FrameLayout android:id="@+id/FrameLayout01" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content">
         <Button android:id="@+id/Button01" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              android:text="button" 
              android:layout_gravity="center_horizontal">
        </Button>
    </FrameLayout>

  </LinearLayout>

Oto projekt, którego szukasz. Spróbuj.

 4
Author: Debarati,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-06 05:31:50

Potrzebowałem dwóch przycisków obok siebie na dole. Użyłem poziomego układu liniowego, ale przypisanie android:layout_height="0dp" i android:layout_weight="0" dla liniowego układu przycisków nie działało. Przypisanie android:layout_height="wrap_content" tylko liniowego układu przycisków. Oto mój roboczy layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

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

        <Button
            android:id="@+id/new_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="New" />

        <Button
            android:id="@+id/suggest_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Suggest" />

    </LinearLayout>

</LinearLayout>
 4
Author: William T. Mallard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-25 19:10:44

RelativeLayout ignoruje atrybuty potomków android:layout_width lub android:layout_height, jeśli dzieci mają atrybuty odpowiednio definiujące ich wartości left i right lub top i bottom.

Aby uzyskać wynik na prawym obrazku, pokazującym listę powyżej przycisku, twój układ powinien wyglądać tak:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@android:id/button1"
        android:layout_alignParentTop="true"/>

    <Button
        android:id="@android:id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@android:string/ok"/>

</RelativeLayout>

Kluczem jest zdefiniowanie android:layout_alignParentTop (definiuje wartość top) i android:layout_above (definiuje wartość bottom) w twoim RecyclerView. W ten sposób RelativeLayout zignoruje android:layout_height="match_parent", a RecyclerView zostanie umieszczony powyżej Button.

Upewnij się również, że przyjrzysz się android:layout_alignWithParentIfMissing, jeśli masz bardziej złożony układ i nadal musisz zdefiniować te wartości.

 2
Author: jpmcosta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-02 18:08:21

Używam Xamarin Android, a moje wymagania są dokładnie takie same jak William T. Mallard, powyżej, tzn. ListView z 2 przyciskami obok siebie pod nim. Rozwiązanie jest takie, że ta odpowiedź nie działała jednak w Xamarin Studio - kiedy ustawiłem wysokość ListView na "0dp", ListView po prostu zniknął.

Mój działający kod Xamarin na Androida wygląda następująco:

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

<ListView
    android:id="@+id/ListView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_above="@+id/ButtonsLinearLayout" />
<LinearLayout
    android:id="@id/ButtonsLinearLayout"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>

Wyrównałem ButtonsLinearLayout do dołu ekranu i ustawiłem ListView na powyżej Buttonslinearlay out.

 1
Author: user1725145,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-27 05:36:07

@jclova jeszcze jedno co możesz zrobić to użyć layout-below=@+id/listviewid w układzie względnym

 0
Author: Girish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-13 09:15:48

W układzie względnym wysokość listview jest match_parent, który jest fill_parent(dla 2.1 i starszych) więc najlepszym rozwiązaniem jest, jeśli chcesz użyć układu względnego, najpierw Zadeklaruj swój przycisk, a następnie Widok listy, Ustaw pozycję widoku listy jak powyżej id przycisku, jeśli chcesz Przycisk zawsze na dole, a następnie ustaw go alignParentBottom.. Snippet is

<RelativeLayout 
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/rl1"><Button 
 android:layout_width="MATCH_PARENT" 
 android:layout_height="WRAP_CONTENT" 
 /><ListView 
 android:layout_width="MATCH_PARENT" 
 android:layout_height="0" 
 android:layout_above="@id/listview"/></RelativeLayout>

Dzięki temu Widok listy nie zajmuje całego miejsca i nie powoduje wyświetlenia przycisku..

 0
Author: om252345,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-13 09:16:00

To będzie najlepsze i najprostsze rozwiązanie problemu. Po prostu dodaj android:layout_above="@id/nameOfId" do układu, który chcesz przenieść powyżej w odniesieniu do tego układu.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sumeru.commons.activity.CommonDocumentUploadActivity">

    <ListView

        android:id="@+id/documentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/verifyOtp" />


    <com.sumeru.commons.helper.CustomButton
        android:id="@+id/verifyOtp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/otp_verification" />
</RelativeLayout>
 0
Author: Rishabh Dhiman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-13 11:25:22