简体   繁体   中英

Add a button to Android list view

I have implemented a simple Android list view using following code.

package com.listTester;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ListTestActivity extends ListActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
String[] STOCK = new String[] {...};

super.onCreate(savedInstanceState);

ListView listView = getListView();
listView.setCacheColorHint(R.drawable.bckimage);
listView.setBackgroundResource(R.drawable.bckimage);
listView.setTextFilterEnabled(true);
getListView().setBackgroundResource(R.drawable.bckimage);



setListAdapter(new ArrayAdapter<String>(this, R.layout.list_stock,STOCK));
     . . .
}
}

The layout code

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:padding="10dp"
 android:textSize="20sp"
 android:gravity="center_vertical|center_horizontal">
</TextView>

Now I want to add buttons below to the list elements as shown in the image(This is an edited image).

这就是我想要的

How can I do this ??Please help me!

Thanks in advance!

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

    <ListView
        android:id="@+id/listView"
        android:layout_width = "fill_parent"
        android:layout_height= "360dp"  
     >
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
    >
        <Button
             android:id = "@+id/btn_check"
             android:text="Check Another"
             android:layout_width = "0dp"
             android:layout_height = "wrap_content"
             android:layout_weight="50"
             android:layout_gravity="center"
        />
        <Button
             android:id = "@+id/btn_ok"
             android:text="OK"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_gravity="center"
             android:layout_weight="50"
        />
    </LinearLayout>
</LinearLayout>

fix the listView heigth

You can use merging style for this You can use http://developer.android.com/resources/articles/layout-tricks-merge.html link for more knowledge about merge.

<?xml version="1.0" encoding="utf-8"?>

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">


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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
         android:layout_gravity="bottom"

    >
        <Button
             android:id = "@+id/btn_check"
             android:text="Check Another"
             android:layout_width = "0dp"
             android:layout_height = "wrap_content"
             android:layout_weight="50"
             android:layout_gravity="center"
        />
        <Button
             android:id = "@+id/btn_ok"
             android:text="OK"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_gravity="center"
             android:layout_weight="50"
        />
    </LinearLayout>

</merge>

Don't try to make them part of the ListView. Activities containing ListView often don't have an XML layout. So instantiate a ListFragment in your Activity, and place the buttons below the Fragment in your layout. Or, alternatively, give the buttons a miss entirely and use ActionBar actions. It's not obvious what your "OK" button does, but it sort of looks like you could just use "Back" for that.

不要使用listactivity...。仅使用activity,并在其中放置一个listview并添加其他组件。

Dont use listactivity....use only activity, and add one more layout xml file and add below code into new layout xml file.

<ListView
    android:id="@+id/mLstView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/mLlayout1" />

<LinearLayout
    android:id="@+id/mLlayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/mBtn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Check Another" />

    <Button
        android:id="@+id/mBtn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/mBtn1"
        android:layout_weight="1"
        android:text="OK" />
</LinearLayout>

and Use Custom Adapter.

You can do this in by using setContentView(R.layout.myxml);

myxml.xml ::

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

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
    >
        <Button
             android:id = "@+id/btn1"
             android:text="Check Another"
             android:layout_width = "0dp"
             android:layout_height = "wrap_content"
             android:layout_weight="50"
             android:layout_gravity="center"
        />
        <Button
             android:id = "@+id/btn2"
             android:text="OK"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:layout_gravity="center"
             android:layout_weight="50"
        />
    </LinearLayout>
</LinearLayout>

Use Activity as modified as per your code. Follow the following xml code...

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

   <ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1">
  </ListView>

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:layout_weight="1">

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

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

  </LinearLayout>

 </LinearLayout>

You should use a footerView. You can do it like this:

ListView listView = getListView();
listView.setCacheColorHint(R.drawable.bckimage);
listView.setBackgroundResource(R.drawable.bckimage);
listView.setTextFilterEnabled(true);
getListView().setBackgroundResource(R.drawable.bckimage);

LayoutInflater layoutInflater = LayoutInflater.from(getContext());

RelativeLayout bottomButtonsLayout = new RelativeLayout(getContext());
    layoutInflater.inflate(R.layout.your_buttons_layout, bottomButtonsLayout, true);
listView .addFooterView(bottomButtonsLayout);

setListAdapter(new ArrayAdapter<String>(this, R.layout.list_stock,STOCK));

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