Android layout - listview and edittext below

2020-05-06 16:58发布

问题:

I'm trying to mimic the behaviour of the HTC SMS application (tradional view), where all messages are shown, and an EditTextis shown below. As you can see in the screenshot, when scrolling upwards, the EditText scrolls away at the bottom.

I'm stuck with this, even after reading multiple posts (eg Android Layout with ListView and Buttons and this website: http://www.finalconcept.com.au/article/view/android-keeping-buttons-visible), it's not working as expected.

Thanks to the comments and EditText now showing under ListView, I've managed to have my ListView take all available space and start scrolling once completed. The EditText is showing at the bottom of the screen now - always. I'd like it to disappear at the bottom when I scroll up though - now it remains at the bottom

Current Code:

<?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">


        <ListView
            android:id="@android:id/android:list"
            android:layout_width="fill_parent" 
            android:layout_height="0dp"
            android:layout_weight="1"
            />

    <TableLayout 
        android:layout_weight="0" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <TableRow>
            <EditText android:id="@+id/newmessagecontent"
                android:layout_height="150dp" 
                android:singleLine="false"
                android:gravity="top"
                android:layout_width="250dp"
                android:layout_alignParentTop="true"
                  />

            <Button android:layout_height="wrap_content" 
                android:id="@+id/sendmessage" 
                android:text="Send" 
                android:layout_width="wrap_content"
                />
        </TableRow>
    </TableLayout>                  

</LinearLayout>

回答1:

i think what you need to implement here is some sort of modification of the SeparatedListAdapter from Jeff Sharkey from this Article. In this article he not only manages to add two Adapters to a ListView but also explains how to have Headers to separate them if you want (you can remove that part of the code).

So what i mean, is your first Adapter will be the data with It's rows, and the second Adapter will be a dummy one with no data that just points to a View with your controls or whatever.

this way the ListView and what you want to add at the bottom are gonna be all scrollable.

Hope this helps.



回答2:

A ListView automatically scrolls if all the items in it take up more space than the view provides. What happens if you remove the ScrollView?