简体   繁体   中英

Android Add Button Top Of Listview

Sorry, I'm a newbie :(

How to add a Button at the top of a ListView and Style like here: This Image

I think i'ts better when you use a LinearLayout first with vertical orientation, and put a Button, and put a ListView. The LinearLayout will your "Content Wrapper" like this: main.xml

<?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">
   <Button 
      android:text="Button"
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" />
   <ListView 
      android:id="@+id/listView1" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:entries="@array/list"/>       
 </LinearLayout>

and a string-array for entries:

<string-array name="list">
    <item>Entry1</item>
    <item>Entry2</item>
    <item>Entry3</item>
    <item>Entry4</item>
</string-array>

You could try ListView.addHeaderView();

This way the button will appear inside the ListView above the elements. Also, have a look at this tutorial .

You can design the button in the own layout who are going inflated in the adapter and set the button visibility gone. In the adapter you must only check in the getview method if the psoition is 0 and if 0, make the button visible else make it gone. This way, you have a button in te top of the list and is scrollable. To make the button clickable, only implement the onclick method in the getview method. Its very easy.

Firstly, I just want to confirm that, is the Button always on top of the ListView that even when ListView is scrolled, the Button is still there or the Button is scrolled over too

If it is the first case, it is pretty simple, just make a vertical LinearLayout with a Button before a ListView

If it is the second case, it is rather complicated. As you shouldn't use ListView inside ScrollView , I suggest you make the first row of the ListView to be a customized view that has a Button inside. In order to do this, check out how to extend BaseAdapter and LayoutInflate .

Maybe this button is not part of the listview. Try to make a layout like this:

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

you can use a relativeLayout to do this :

<RelativeLayout android:id="@+id/parentLayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

   <Button android:id="@+id/btn"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="My Button"
      android:alignParentTop = "true"/>

  <ListView android:id="@+id/myList"
     android:layout_below="@id/btn"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_gravity="top|center_horizontal"
    android:layout_width="match_parent" android:layout_height="wrap_content">
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:text="Add Alarm"/>
    <ListView android:layout_width="fill_parent" android:layout_height="0dip"
        android:layout_weight="1.0"></ListView>
</LinearLayout>

This should give the desired layout.

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