Android 嵌套布局中的焦点和单击事件
我的布局如下:Android 嵌套布局中的焦点和单击事件,android,onclick,onclicklistener,Android,Onclick,Onclicklistener,我的布局如下: <RelativeLayout android:layout_height="30dp" android:layout_width="100dp" android:background="@drawable/bg" android:clickable="true" android:focusable="true"> <Button android:id="@+id/btn" and
<RelativeLayout
android:layout_height="30dp"
android:layout_width="100dp"
android:background="@drawable/bg"
android:clickable="true"
android:focusable="true">
<Button
android:id="@+id/btn"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:focusable="false"
android:text="Click"
android:layout_centerInParent="true" />
当我点击按钮时,行为改变了,如果我点击按钮,它的父母们的布局不会改变,只有点击空白区域,
RelativeLayout
中,是否有其他选择
更新完整的layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<LinearLayout
android:background="@drawable/bg"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:text="Name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TextView
android:text="Detail"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<RelativeLayout
android:background="@drawable/bg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/btn"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:focusable="false"
android:text="Action1" />
</RelativeLayout>
<TextView
android:layout_height="20dp"
android:layout_width="1dp"
android:text="|"
android:layout_weight="0"
android:layout_marginRight="5dp" />
<RelativeLayout
android:background="@drawable/bg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/btn2"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:focusable="false"
android:text="Action2" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
现在我有两个问题:
1单击文本时,按钮不会更改其背景
< p > 2,如果单击<代码> BTN1的空白区域,则<<代码> BTN1和<代码> BTN2将改变其背景。
目前的影响:
如果视图不可单击,则将使其可单击。另一种方法可能是直接操纵属性。当子元素使用单击事件时,其父布局将不会被委派该操作,如果希望父RelativeLayout处理单击事件,则覆盖RelativeLayout的单击侦听器,不是按钮。如果你想要这样的行为,你不应该有两个视图。。有什么特别的原因让你想要两个视图吗?一种方法是使按钮
match\u parent
,因为RelativeLayout
有一个在聚焦时具有背景色的父按钮,我不希望此背景覆盖按钮。。如果发布完整的xml,那就太好了。我想看看布局(如你所说的家长)。。调试…@AmulyaKhare:我更新了我的帖子。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<LinearLayout
android:background="@drawable/bg"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:text="Name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TextView
android:text="Detail"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:clickable="true"
android:focusable="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<RelativeLayout
android:background="@drawable/bg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/btn"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:focusable="false"
android:text="Action1" />
</RelativeLayout>
<TextView
android:layout_height="20dp"
android:layout_width="1dp"
android:text="|"
android:layout_weight="0"
android:layout_marginRight="5dp" />
<RelativeLayout
android:background="@drawable/bg"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/btn2"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:clickable="false"
android:focusable="false"
android:text="Action2" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>