AlertDialog中的自定义ListView仅可单击TextView

时间:2015-03-07 22:09:40

标签: android xml android-layout listview android-listview

无法理解为什么ListView在AlertDialog中无法点击,textView只能在行中点击

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            alertDialog = new AlertDialog.Builder(MyClass.this);
            LayoutInflater inflater = getLayoutInflater();
            View convertView = inflater.inflate(R.layout.custom, null);
            alertDialog.setView(convertView);
            alertDialog.setTitle(MyClass.this.getResources().getString(R.string.age));
            ListView lv = (ListView) convertView.findViewById(R.id.list);
            alertDialog.setCancelable(true);
            adapter = new CustomAdapter(this, ages, "fonts/comicrelief.ttf"); 
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             .................................
            });
            dialog = alertDialog.show();
        }
    });

这是自定义xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">

<TextView
    android:id="@+id/label11"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center_vertical"
    android:paddingLeft="10dp"
    android:textSize="25sp"></TextView>

和CustomAdapter:

public class CustomAdapter extends ArrayAdapter<CharSequence> {
Context context;
String data[] = null;
Typeface tf;

public CustomAdapter(Context context, String[] data, String FONT ) {
    super(context, R.layout.row_layout, data);
    this.context = context;
    this.data = data;
    tf = Typeface.createFromAsset(context.getAssets(), FONT);
}

@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.row_layout, parent, false);
    TextView textView = (TextView)rowView.findViewById(R.id.label);
    textView.setText(data[position]);
    textView.setTypeface(tf);
    return rowView;
}}

当我点击ListView中的项目时,只有textView可以点击并且所有工作都很好,但是我想要点击整行的区域,而不仅仅是textView。任何想法我怎么能做到?

1 个答案:

答案 0 :(得分:0)

这是我的问题的解决方案。我应该在ListView xml中使用fill_parent:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true">
</ListView>