带有Button的Android ListView无法选择(可点击)

Ali*_*Ali 8 android listview xml-layout

我面临一个奇怪的问题,我在我的ListView中添加了一个自定义行,当我删除Button行是可选择的,但是当我添加Button我无法点击该行时,请参阅下面的xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="77dp"
    android:layout_height="77dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:src="@drawable/company_logo" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Idds  sdsad "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/imageView1"

    android:layout_below="@+id/textView1"
    android:textColor="#8b8989"
    android:layout_marginLeft="5dp"
    android:text="Tap to see detail"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:text="Button" />
Run Code Online (Sandbox Code Playgroud)

请帮助解释为什么会这样.

Pra*_*ani 26

在使用自定义行时.

设置完成后onclickListenerbuttongetView设置了focusability false.

button.setFocusable(false);

android:descendantFocusability="blocksDescendants"为您的行的布局容器设置.您可以直接设置,android:focusable="false"但这会使您的按钮无法点击.

  • 在行的布局容器上设置 android:descendantFocusability="blocksDescendants" 足以为我解决这个问题。我不必对按钮的可聚焦属性做任何事情。 (10认同)

Car*_*nal 20

尝试设置

android:focusable="false"
android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

Button的xml.将Button在该行获取焦点,这就是为什么你不能选择你行.