简体   繁体   中英

scroll a layout with a listview android

My problem is that i have my listview working perfect on a absoulteLayout but the buttons on the bottom aren't show up! I put a scrollview with a absoluteLayout with all items (textview, button, etc) and outside of the scrollview I put the listview, this didn't work, either, just scroll the buttons but the listview just move a little bit, how can I put a scrollview to can see the buttons on the button and make the listview works? my XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->

    <AbsoluteLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Ab">

        <ListView
            android:layout_width="317dp"
            android:layout_height="429dp"
            android:id="@+id/listView"
            android:layout_gravity="center"
            android:layout_x="45dp"
            android:layout_y="41dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This Works Monica!!"
            android:id="@+id/TV1"
            android:layout_gravity="center_horizontal"
            android:layout_x="120dp"
            android:layout_y="22dp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Play :D"
            android:id="@+id/TESTME"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_x="190dp"
            android:layout_y="520dp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Pause"
            android:id="@+id/PAUSE"
            android:layout_x="110dp"
            android:layout_y="521dp" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="37dp"
            android:text="Lista"
            android:id="@+id/LISTA"
            android:layout_x="274dp"
            android:layout_y="520dp" />

        <CheckBox
            android:layout_width="84dp"
            android:layout_height="wrap_content"
            android:text="Lista?"
            android:id="@+id/CHECARL"
            android:layout_x="267dp"
            android:layout_y="490dp"
            android:checked="false" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="BorrarL"
            android:id="@+id/BORRARL"
            android:layout_x="20dp"
            android:layout_y="520dp" />
    </AbsoluteLayout>

<!-- The navigation drawer -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#E6E6E7" />

You are using absolute layout that means, from documentation:

A layout that lets you specify exact locations (x/y coordinates) of its children.

So if you don't see the buttons it is because they are positioned outside of your device screen bounds. And their position is absolute / fixed so they stay there no matter what.

I would recommend to change to different layout. I don't know what kind of app you are building but absolute layout is generally not the best choice.

Possibility number one:

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

                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/background"
                android:clickable="true">


    <ListView
        android:id="@+id/allPaymentListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/addNewPayment"

        android:layout_marginTop="10dp"></ListView>

    <Button
        android:id="@+id/addNewPayment"
        android:layout_width="match_parent"
        android:layout_height="@dimen/primary_button_height"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:background="@drawable/blackbutton"
        android:text="@string/add_new_card"
        android:textColor="@color/light_blue"/>


</RelativeLayout>

In this case the button will be always seen on the bottom of the page, below the listview.

The second possibilty is to add a footer to the listview: http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

In that case the view with the button will be the last element in the listview.

And don't use Absolute Layout it is deprecated, the reason is the app will not scale to different screen sizes as you wanted.

You should use a RelativeLayout with the buttons at the bottom and the ListView placed on top of the buttons.

AbsoluteLayout is, in general. to be avoided for layouts unless you are trying to accomplish a very specific purpose. It's considered an anti-pattern to position elements absolutely. To achieve what you have stated, a RelativeLayout would be the way to go.

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