Android listview中添加button
android 中所有的控件都是继承view,如果你认为你有能力,你可以修改framework,你可以把任何控件嵌入到别的控件中。
1.第一你要创建一个线性布局,水平的,一个textview,一个button.
我直接上源码:
public class TestItemView extends LinearLayout {
Context context;
private TextView text;
private Button btn;
private LayoutParams a ;
Drawable getIcon(int resource) {
return getResources().getDrawable(resource);
}
public TestItemView(Context context,String title,String btnString) {
super(context);
a=new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
a.leftMargin=150;
this.context=context;
this.setOrientation(HORIZONTAL);
text = new TextView(context);
text.setText(title);
text.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.icon),
null, null, null);
text.setTextSize(20);
text.setGravity(17);
addView(text, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
btn = new Button(context);
btn.setText(btnString);
addView(btn,a);
//处理button点击事件
Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
text.setText("我好!");
}
};
btn.setOnClickListener(mOkOnClickListener);
// TODO Auto-generated constructor stub
}
public void setTitle(String title)
{
text.setText(title);
}
public void setButton(String btnString)
{
btn.setText(btnString);
}
2.你要创建一个BaseAdapter,将你的布局加入到一个view中。
public class textAdapter extends BaseAdapter{
public Context context;
public List< ImageItem > items;
public textAdapter(Context ctx) {
context = ctx;
items = new ArrayList< ImageItem >();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TestItemView iiv;
if (convertView==null)
{
iiv = new TestItemView(context, items.get(position).text,items.get(position).btnString);
}
else
{
iiv = (TestItemView)convertView;
iiv.setTitle(items.get(position).text);
iiv.setButton(items.get(position).btnString);
}
return iiv;
}
3.创建一个类,也算是一个结构体,为了传参方便
public class ImageItem {
public String text;
public String btnString;
public ImageItem(String text,String btnString) {
super();
this.text = text;
this.btnString = btnString;
}
}
4.将你的view显示在listview中
public class testList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textAdapter adapter = new textAdapter(this);
adapter.items.add(new ImageItem("你好","开始"));
adapter.items.add(new ImageItem("你好","开始"));
adapter.items.add(new ImageItem("你好","开始"));
adapter.items.add(new ImageItem("你好","开始"));
adapter.items.add(new ImageItem("你好","开始"));
setListAdapter(adapter);
}
}
5.关于res/layout/main.xml定义你所用到的控件view.
< ?xml version="1.0" encoding="utf-8"? >
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
< Button android:id="@+id/icon"
android:layout_width="48dip"
android:layout_height="48dip" / >
< TextView android:id="@+id/text"
android:layout_gravity="center_vertical"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content" / >
< /LinearLayout >
总结,你可以通过布局来实现listview中加入任何控件,只要你喜欢