如何使一个RelativeLayout可点击?

12

我有一个像这样的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true"
    android:clickable="true"
    android:duplicateParentState="true"
    android:focusable="true"
    android:paddingBottom="10dip" >

    <ImageView
        android:id="@+id/imagView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:background="@drawable/imageView_bg"
        android:focusableInTouchMode="false" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="18dip"
        android:background="@drawable/button_bg"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="left|center_vertical"
        android:paddingLeft="25dip"
        android:textColor="#ffffff"
        android:textSize="20dip" />

</RelativeLayout>

按钮背景.xml

  <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/btn_pressed" 
            android:state_pressed="true"/>
        <item android:drawable="@drawable/btn_normal"/>

    </selector>

imageView_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img_pressed" 
        android:state_pressed="true"/>
    <item android:drawable="@drawable/img_normal"/>

</selector>

图像视图和按钮有按下的图片。当用户点击视图时,想同时按下图像视图和按钮(显示按下的图片)。但是它没有显示按下的图片。如何实现?问题出在哪里?

提前感谢。


1
你是否将一个 OnClickListener 附加到了你的 RelativeLayout 上? - Shark
它们是否堆叠在一起? - njzk2
1
将图像视图替换为一个布局,在其中放置按钮,并向按钮添加重复父状态。 - njzk2
3个回答

18
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainLayout);

rl.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {}
 });

如果你遇到了"RelativeLayout cannot be resolved to a type"的问题,请确保在你的java文件顶部导入以下两个包:import android.widget.RelativeLayout; 和 import android.view.View; - jwinn
1
你不能只设置 android:clickable="true" 吗? - Boris Treukhov
还要不要忘记在RelativeLayout内的视图中添加android:clickable="false"。 - oiyio

5
我认为它解决了问题。
((RelativeLayout)findViewById(R.id.mainLayout)).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
    ViewGroup viewGroup = (ViewGroup) view;
     for (int i = 0; i < viewGroup .getChildCount(); i++) {

       View viewChild = viewGroup .getChildAt(i);
       viewChild.setPressed(true);

          }
    }
});

0

简单。在FrameLayout中放置RelativeLayout。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接