2012-08-17 71 views
0

我有一個奇怪的問題。我有一個繼承自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_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" /> 

,這裏是我的根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的可真有怪異的行爲有時...

+1

你試過invalidate()嗎? – sandrstar 2012-08-17 13:17:36

+0

有沒有任何錯誤?發表您的logcat – JiTHiN 2012-08-17 13:19:11

+0

與無效沒有運氣() – rmonjo 2012-08-17 13:41:15

回答

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> 
+0

謝謝,我編輯我的問題 – rmonjo 2012-08-17 13:32:52

+0

你能告訴代碼你在哪裏充氣這個XML?像膨脹(上下文,R.layout.my_layout,這個)? – 2012-08-17 13:34:53

+0

字符串服務=上下文。LAYOUT_INFLATER_SERVICE; \t \t LayoutInflater li =(LayoutInflater)getContext()。getSystemService(service); \t \t li.inflate(R.layout.other_event_cell,this); – rmonjo 2012-08-17 13:36:31

0

的一個問題,我看到的,是System.out.println("Action down");在您的兩個條件複製。這可能會在嘗試查找日誌中的問題時導致問題。


而且,你說:

我嘗試當它接觸了在運行時改變它...

如果你想只是改變它時,它被觸摸,請嘗試使用OnClickListener而不是您正在使用的Touch


另外,你的第二個條件是MotionEvent.ACTION_CANCEL,是,應該是ACTION_UP

+0

OnCLick發生在觸摸被釋放時,我希望它在啓動時發生。是的,我的日誌很糟糕,但這不是問題 – rmonjo 2012-08-17 13:38:29