相对布局可点击且可聚焦

时间:2017-05-30 08:49:13

标签: android xml nested

enter image description here它可能是重复的,我搜索了答案,但不幸的是我没有找到答案.. 我有相对布局与表行和图像视图和文本视图嵌套

<!--  add photo image  -->
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:layout_weight="1.5"
    android:id="@+id/layot_attach"
    android:clickable="true"
    android:focusable="true"
    android:background="@drawable/photo_button_selector"
    android:layoutDirection="rtl">



    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/ic_add_photo_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="5dp"
            android:focusable="false"
            android:clickable="false"
            android:src="@drawable/ic_attach_file_white_48dp"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal|center_vertical"
            android:textColor="@color/white"
            android:id="@+id/textView_attach"
            style="@style/title"
            android:padding="5dp"
            android:layout_weight="1" />


        <ImageView
            android:id="@+id/preview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:padding="5dp" />
    </TableRow>
</RelativeLayout>

当我点击布局时我的方法被调用,除非我点击第一个嵌套图像视图(ic_add_photo_image)
我将可点击和可聚焦的虚假值添加到图像视图中,但有些工作

这是我的选择器 的 photo_button_selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
      android:drawable="@drawable/photo_button_pressed" /> <!-- pressed -->

<item android:drawable="@drawable/photo_button" /> <!-- default -->

我是否想念任何东西,有人可以帮我解决或解释吗?

这是我的清单:

layoutattach = (RelativeLayout) findViewById(R.id.layot_attach);
layoutattach.setOnClickListener(this); // method called 

编辑

我没有按照我想要的方式解决问题 我在代码中声明了我的图像图标并将alistenr附加到它..我失去了按钮点击的效果..但是现在这样做了

2 个答案:

答案 0 :(得分:0)

请尝试以下解决方案在Relative Layout中添加以下属性: -

android:background="?android:attr/selectableItemBackground"

答案 1 :(得分:0)

我觉得你需要这样, 布局可点击或其他元素的完整部分,所以试试这个。

您的父布局(相对布局)具有以下属性: -

android:descendantFocusability="blocksDescendants"