无法更改RelativeLayout背景onTouch

时间:2012-08-17 13:10:04

标签: android background runtime relativelayout

我有一个奇怪的问题。我有一个继承自RelativeLayout的类。我膨胀成一个xml。此布局的默认背景图像在xml中设置,我尝试在触摸时在运行时更改它:

if(event.getAction() == MotionEvent.ACTION_DOWN) {
    System.out.println("Action down");
    this.setBackgroundResource(R.drawable.event_cell_selected);
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
    System.out.println("Action down");
    this.setBackgroundResource(R.drawable.event_cell);
}

资源event_cellevent_cell_selected都是.png文件。另外,我看到了我的日志。我真的不能说这里发生了什么。

[编辑] 谢谢你的快速回答。在logcat中没有错误,这是我膨胀的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/event_cell"
android:paddingBottom="@dimen/padding_small"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="@dimen/padding_large" >

<TextView
    android:id="@+id/textViewMonth"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="juil."
    android:textColor="@color/event_header"
    android:textSize="19dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textViewDay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/textViewMonth"
    android:layout_marginBottom="@dimen/margin_small"
    android:text="TextView"
    android:textColor="@color/event_header"
    android:textSize="15dp" />

<TextView
    android:id="@+id/textViewTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="TextView"
    android:textColor="@color/event_header"
    android:textSize="17dp" />

<ImageView
    android:id="@+id/imageViewSeparator"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/textViewDay"
    android:layout_marginBottom="@dimen/margin_small"
    android:src="@drawable/event_title_descr_separator" />

<TextView
    android:id="@+id/textViewDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/imageViewSeparator"
    android:ellipsize="end"
    android:maxLines="2"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="12dp" />

这是我的root xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/navigationBarView" >

    <LinearLayout
        android:id="@+id/eventsLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="@dimen/margin_small"
        android:paddingRight="@dimen/margin_small" >

    </LinearLayout>
</ScrollView>

膨胀的RelativeLayout被添加到scrollView的linearlayout

[编辑] 好的,为了解决这个问题,我终于以编程方式(不是通过xml)最终设置我的默认背景图像。这就是诀窍。 Android有时可能会有奇怪的行为......

2 个答案:

答案 0 :(得分:0)

显示布局xml并扩充代码。我打赌问题如下:在你xml你使用RelativeLayout作为根。这意味着然后膨胀你正在将在xml中定义的相对布局添加到在代码中创建的相对布局(由您自己继承自RelativeLayout的类定义)。这意味着xml中定义的背景显示在代码中更改的背景上方。尝试在布局根目录中使用<merge>而不是<RelativeLayout>

您可以在此处阅读“合并”布局标记 http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/event_cell"
    android:paddingBottom="@dimen/padding_small"
    android:paddingLeft="14dp"
    android:paddingRight="14dp"
    android:paddingTop="@dimen/padding_large" >

    <TextView
        android:id="@+id/textViewMonth"
        android:textStyle="bold" />

   .............

</merge>

答案 1 :(得分:0)

我看到的一个问题是System.out.println("Action down");在两种情况下都是重复的。在尝试查找日志中的问题时,这可能会导致问题。


另外,你说:

  

我尝试在触摸时在运行时更改它......

如果您只想在触摸时更改它,请尝试使用OnClickListener代替您正在使用的Touch内容。


此外,您的第二个条件是MotionEvent.ACTION_CANCEL,应该是ACTION_UP吗?

相关问题