【问题标题】:Custom ListView in AlertDialog clickable TextView only仅 AlertDialog 中的自定义 ListView 可点击 TextView
【发布时间】:2015-05-09 08:18:24
【问题描述】:

不明白为什么 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>

和自定义适配器:

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。有什么想法我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    这是我的问题的解决方案。我应该在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      相关资源
      最近更新 更多