【问题标题】:How to make a Relativelayout clickable?如何使相对布局可点击?
【发布时间】:2013-01-25 11:53:41
【问题描述】:

我有这样的布局

<?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>

button_bg.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>

图像视图和按钮已按下图像。当用户单击视图时,想要同时按下图像视图和按钮(显示按下的图像)。但它不显示按下的图像。如何做到这一点?问题出在哪里?

提前致谢

【问题讨论】:

  • 您是否将 OnClickListener 附加到您的 RelativeLayout?
  • 它们是堆叠在一起的吗?
  • 将图像视图替换为您放置按钮的布局,并将重复的父状态添加到按钮

标签: android android-layout styles


【解决方案1】:
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainLayout);

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

【讨论】:

  • 如果您收到““RelativeLayout 无法解析为类型”,请确保您在 java 文件的顶部导入以下内容:import android.widget.RelativeLayout; 和 import android.view .查看;
  • 你不能设置android:clickable="true"吗?
  • 也不要忘记将 android:clickable="false" 添加到 relativeLayout 中的视图。
【解决方案2】:

我认为它解决了问题

((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);

          }
    }
});

【讨论】:

    【解决方案3】:

    Simple.Place RelativeLayout 在 FrameLayout 中。

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多