简体   繁体   中英

Making linearlayouts stay at bottom of screen in android

I am making an android application that uses these views:

(Root) - LinearLayout (Child of root) - ListView + 3 other linearlayouts beneeth that listview. But once i add to many items to the listview, the layouts are being placed outside of the screen. Which i don't want to. How can i make this happen? So that all of the three linearlayouts stay on the screen, at the bottom??? Please help and thanks in advance!

My screen XML code:

<?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:background="@drawable/background"
android:orientation="vertical" >




<TextView
    android:id="@+id/NotesWelcomeTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/NotesWelcomeText" />








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

</ListView



<LinearLayout
    android:id="@+id/DeleteAllItemsFromListViewLinearLayout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:visibility="invisible" >


    <Button
        android:id="@+id/CancelButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Cancel" />


    <Button
        android:id="@+id/DeleteAllButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Delete" />

</LinearLayout>



<LinearLayout
    android:id="@+id/DeleteItemFromListViewLinearLayout"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:visibility="invisible" >


    <Button
        android:id="@+id/CancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Cancel" />


    <Button
        android:id="@+id/DeleteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Delete" />

</LinearLayout>





<LinearLayout
    android:id="@+id/AddItemToListViewLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" >



    <EditText
        android:id="@+id/AddItemToListViewEditText"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </EditText>



    <Button
        android:id="@+id/AddItemToListViewButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Add" />

</LinearLayout>

</LinearLayout>

Try this, you'll have to add the string references and background again, I removed them so I wouldn't have errors

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

<TextView android:id="@+id/NotesWelcomeTextView"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="NotesWelcomeText" />

<ListView android:id="@+android:id/list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_above="@+id/AddItemToListViewLinearLayout"
    android:layout_below="@+id/NotesWelcomeTextView">

</ListView>

<LinearLayout android:id="@+id/DeleteAllItemsFromListViewLinearLayout"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="1" android:visibility="invisible"
    android:layout_alignParentBottom="true">

    <Button android:id="@+id/CancelButton2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Cancel" />

    <Button android:id="@+id/DeleteAllButton" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Delete" />

</LinearLayout>

<LinearLayout android:id="@+id/DeleteItemFromListViewLinearLayout"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="1" android:visibility="invisible"
    android:layout_alignParentBottom="true">

    <Button android:id="@+id/CancelButton" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Cancel" />

    <Button android:id="@+id/DeleteButton" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Delete" />

</LinearLayout>

<LinearLayout android:id="@+id/AddItemToListViewLinearLayout"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:visibility="gone" android:layout_alignParentBottom="true">

    <EditText android:id="@+id/AddItemToListViewEditText"
        android:layout_width="250dp" android:layout_height="wrap_content"
        android:layout_weight="1">
    </EditText>

    <Button android:id="@+id/AddItemToListViewButton"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="1" android:text="Add" />

</LinearLayout>

Give your ListView a weight of 0 and set its height to 0. This will tell the ListView to only take up the space that is not use by other views.

<ListView ...
    android:layout_weight="0"
    android:layout_height="0"
    ...
    />

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