我有一個奇怪的問題。我有一個繼承自RelativeLayout
的類。我膨脹到一個XML。此佈局的默認背景圖片在XML設置,我嘗試當它接觸了在運行時改變它:無法更改RelativeLayout背景onTouch
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_cell
和event_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" />
,這裏是我的根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被添加到了滾動的的LinearLayout
[編輯] 好的解決這個概率lem我終於最終以編程方式設置我的默認背景圖片(不是通過xml)。這就是訣竅。 Android的可真有怪異的行爲有時...
你試過invalidate()嗎? – sandrstar 2012-08-17 13:17:36
有沒有任何錯誤?發表您的logcat – JiTHiN 2012-08-17 13:19:11
與無效沒有運氣() – rmonjo 2012-08-17 13:41:15