我正在尝试在列表视图中添加一个按钮,我在谷歌搜索了很多,但对我来说没有什么是好的。
这是我的代码:我有两个类:
Menu.java
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Menu extends ListActivity implements OnItemClickListener {
String[] listaMeniu = { "1", "2", "3"};
Button butonNota;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ListAdapter(this, listaMeniu));
ListView listView = getListView();
listView.setOnItemClickListener (this);
Button btnLoadMore = new Button(this);
btnLoadMore.setText("show me");
}
}
menu.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:padding="5dp">
<ImageView
android:id="@+id/1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:src="@drawable/1" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "30dp"
android:text="1" />
</LinearLayout>
ListAdapter.java
package com.example.a;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter {
private Context context;
private String[] values;
public ListAdapter(Context context, String[] values) {
// TODO Auto-generated constructor stub
super (context, R.layout.menu, values);
this.context = context;
this.values = values;
}
}
我已经制作了列表视图,但我不知道如何在列表上方添加按钮。我尝试在menu.xml中添加它,但是它会显示列表中每个项目的按钮。希望你们明白我的意思。 谢谢!
答案 0 :(得分:1)
最好的方法是创建自定义适配器。例如:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
Button buttonView = (Button) rowView.findViewById(R.id.button);
buttonView.setText(values[position]);
return rowView;
}
}
这是来自“rowlayout.xml”的xml。您必须将布局文件放在res / layout项目文件夹中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px">
</Button>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
然后只需更新你的Menu.java
public class Menu extends ListActivity implements OnItemClickListener {
String[] listaMeniu = { "1", "2", "3"};
Button butonNota;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new MySimpleArrayAdapter(this, listaMeniu));
...
}
}
答案 1 :(得分:0)
您需要在适配器中覆盖方法getView以返回Button。
答案 2 :(得分:0)
您希望列表中的第10个项目成为按钮。
这意味着当您覆盖ArrayAdapter类时,需要修改GetView(),以便返回按钮而不是图片。创建两个不同的布局XML文件,rowlayout_picture.xml和rowlayout_button.xml,然后:
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
if (position < 10)
rowView = inflater.inflate(R.layout.rowlayout_picture, parent, false);
else
rowView = inflater.inflate(R.layout.rowlayout_button, parent, false);
return rowView;
}