0

奇妙な問題があります。から継承するクラスがあり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" />

そして、ここに私のルート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は本当に奇妙な動作をすることがあります...

4

2 に答える 2

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>
于 2012-08-17T13:21:40.293 に答える
0

私が見る1つの問題はSystem.out.println("Action down");、両方の条件で重複していることです。これにより、ログで問題を探す際に問題が発生する可能性があります。


また、次のように言います。

触れたときに実行時に変更しようとしています...

触れたときに変更したい場合は、使用しているもののOnClickListener代わりにを使用してみてくださいTouch


また、2 番目の条件は ですがMotionEvent.ACTION_CANCEL、それはACTION_UPでしょうか?

于 2012-08-17T13:24:34.697 に答える