2013-11-05 115 views
1

重点和点击事件我有以下布局:在嵌套布局

<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集中单击该按钮时,上面的代码工作正常。

无论我点击文字还是空白区域,RelativeLayout都会改变其背景颜色。

然而,当我添加的onclick监听器按钮这样的:

btn = (Button) findViewById(R.id.btn); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     } 
    }); 

然后behavor改变,当我按一下按钮,如果我按一下按钮其父的布局并没有改变,只有当我点击空白区域,RelativeLayout会改变背景。

看到这张照片(他们都捕捉按下鼠标时):

enter image description here

我不知道是否有任何文档来解释这一现象?

现在,我必须直接将点击侦听器添加到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按钮不会改变它的背景。

2如果我点击btn1的空白区域,那么btn1btn2都将更改其背景。

影响的时刻:

enter image description here

+0

如果你想要这样的行为,你不应该有两个视图..你为什么要两个视图的任何特定原因?一个黑客可能会做按钮,'match_parent' –

+0

因为'RelativeLayout'有一个父母有聚焦背景色,我不希望这个背景覆盖'Button' – hguser

+0

嗯..这将是很好,如果你发布完整的XML。我想看到布局(例如,你正在谈论的家长).. ..调试.. –

回答

0

当子元素消耗单击事件时,如果您希望父级的RelativeLayout处理单击事件,那么将不会为其父级布局委派操作,然后重写RelativeLayout的单击侦听器,而不是按钮。