1

StateListDrawable に奇妙な問題があるか、(おそらく) 何か不足している可能性があります。そのためのテスト アプリケーションを作成しましたが、同じ問題が発生します。したがって、これはファイル test_selection.xml の StateListDrawable リソースです。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_selected="true">
  <shape android:shape="rectangle" android:background="#ff0000">
   <corners android:radius="10dp" />
   <gradient android:startColor="#ff5555"
    android:endColor="#ff5555" android:angle="0" />
  </shape>
 </item>
 <item android:state_selected="false">
  <shape android:shape="rectangle" android:background="#eeeeee">
   <corners android:radius="10dp" />
   <gradient android:startColor="#eeeeee"
    android:endColor="#eeeeee" android:angle="0" />
  </shape>
 </item>
</selector>

これは非常に単純なセレクタで、選択された状態を赤色で、選択されていない状態を白色で表します。

私の main.xml テンプレートはとてもシンプルです。選択範囲を背景として使用する TextView を使用するだけです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="@string/hello"
  android:textSize="30dp" android:id="@+id/test_view_example" android:background="@drawable/test_selection"/>
 <Button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/refresh"
  android:onClick="updateView" android:text="refresh"></Button>
</LinearLayout>

私のアクティビティ コードも非常に単純です。

public class TestDrawableStateActivity extends Activity {

 private final static int[] SELECTED_STATE = { android.R.attr.state_selected };
 private final static int[] UNSELECTED_STATE = {};

 private TextView textView; 

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  textView = (TextView) findViewById(R.id.test_view_example);
 }

 @Override
 protected void onResume() {
  super.onResume();
  // Carichiamo la Drawable 
  if(textView.getBackground().setState(SELECTED_STATE)){ 
   textView.invalidate();
  }  
 }

 public void updateView(View view) {
  if(textView.getBackground().setState(SELECTED_STATE)){
   textView.invalidate();
  };
 }
}

Activity が開始したら、Drawable (StateListDrawable) の状態を SELECTED の値で設定しようとします。それはすべて非常に単純に思えます....しかし問題は、状態が表示されないことです。後でボタンをクリックしてメソッド updateView() を実行すると、状態が変化します。私の問題はどこですか?どこが間違っていますか?

どうもありがとうマックス

4

1 に答える 1

0

最後に、解決策を見つけました。同じはずですが、そうではありません。この onCreate() バージョンを使用しました

/** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  textView = (TextView) findViewById(R.id.test_view_example);
  textView.setSelected(true);
 }

驚くべきことに、呼び出しは での呼び出しとはsetSelected(true) 異なります。これは内部ビュー状態によるものです。setState()int[]{android.R.attrs.state_selected}

バイマックス

于 2010-08-26T13:40:17.380 に答える