简体   繁体   中英

Android: ListView on top of other elements

My layout looks like I want it to... up to the point that I get many items in the list and then the list covers the Edittext and Button elements.

I can't see what I have wrong. And when I set the ListView android:layout_height="fill_parent" it covered my buttons even when the list was empty.

This should be a pretty simple layout. I have spent over an hour messing with it.

Any help?

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/txt_editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/str_addSnippetHint" />

    <Button
        android:id="@+id/btn_addSnipet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/addSnippet" />
</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/txt_editText"
    android:layout_alignParentTop="true"
    android:drawSelectorOnTop="false" />

put this inside LinearLayout tag after Button

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:drawSelectorOnTop="false" />


Remove this line   android:layout_above="@id/txt_editText"

And if you want to show listView above Button then Use Relative layout.And then set relativity

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
 <EditText
    android:id="@+id/txt_editText"
    android:layout_width="fill_parent" android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_height="wrap_content"
    android:hint="@string/str_addSnippetHint" />
 <Button  android:id="@+id/btn_addSnipet" android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/addSnippet" />
<ListView android:id="@android:id/list"
android:layout_width="fill_parent" android:layout_above="@+id/btn_addSnipet"
android:layout_height="wrap_content"     android:layout_above="@id/txt_editText"
android:layout_alignParentTop="true"    android:drawSelectorOnTop="false" />
</RelativeLayout>

Add your ListView inside the LinearLayout instead of creating separate Layouts..

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/txt_editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/str_addSnippetHint" />

    <Button
        android:id="@+id/btn_addSnipet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/addSnippet" />
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/txt_editText"
    android:layout_alignParentTop="true"
    android:drawSelectorOnTop="false" />

</LinearLayout>

try this in place of your code

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:orientation="vertical" >

 <EditText
   android:id="@+id/editText"
   android:layout_width="fill_parent"  
   android:layout_alignParentLeft="true"
   android:layout_height="wrap_content"
   android:hint="edittext" />
    <Button
      android:id="@+id/btn_addSnipet"  
      android:layout_alignParentRight="true"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:text="button" />
   <ListView
      android:id="@android:id/list"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/txt_editText"
      android:layout_alignParentTop="true"
      android:drawSelectorOnTop="false" />
 </LinearLayout>

trythis :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" android:orientation="vertical">

    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentTop="true"
        android:drawSelectorOnTop="false" />

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:layout_above="@id/list">
        <EditText android:id="@+id/txt_editText"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_weight="1" android:hint="@string/str_addSnippetHint" />

        <Button android:id="@+id/btn_addSnipet" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_weight="1"
            android:layout_below="@id/txt_editText" android:text="@string/addSnippet" />
    </RelativeLayout>

</RelativeLayout>

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