在我的应用程序中,我想将按钮添加到顶部的列表视图。在这种情况下,它显示每个列表项而不是顶部的按钮。如何只在顶部显示
gender.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"
>
<Button
android:id="@+id/b1
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView android:id="@+id/text"
android:layout_gravity="center_vertical"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Egender.java
package com.Elgifto;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
public class Egender extends ListActivity {
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return GENDER.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.gender, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.cb=(CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(GENDER[position]);
//holder.text.setTextColor(Color.BLACK);
if(holder.cb.isChecked())
{
holder.cb.setChecked(false);
String st=GENDER[position];
System.out.println("String:"+st);
}
return convertView;
}
static class ViewHolder {
TextView text;
CheckBox cb;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}
private static final String[] GENDER = new String[] {
"Male","Female"
};
}
感谢
答案 0 :(得分:0)
除非我弄错了,否则您将使用gender.xml作为ListView中每个行/项的布局。因此,您可能希望从gender.xml布局中删除Button声明。
如果你想要将Button固定在ListView上面,那么你可能想要编辑声明ListView的布局,也可以声明Button,假设你有这样的xml布局。
例如,带有ListView的xml布局可能看起来如下所示。
<LinearLayout orientation="horizontal">
<Button
id="my_button"
width="wrap_content"
height="wrap_content"
layout_gravity="center"
text="my button" />
<ListView
id="my_listview"
width="fill_parent"
height="0dip"
weight="1" />
</LinearLayout>
(注意:此xml布局不完整,缺少部分,例如xmlns声明。有关更完整的示例,请查看Scrolling Text Above Buttons, Buttons Fixed At Bottom。)
答案 1 :(得分:0)
您还可以将标题添加到列表视图中:
Button btn=new Button(this);
list.addHeaderView(btn);
这将使列表视图的第一项
您也可以通过
使其无法选择list.addHeaderView(vbtn, null, false);
并发送了一个onItemClickListener。