简体   繁体   English

Android刷新StateListDrawable问题

[英]Android refresh StateListDrawable problem

I have a strange problem with StateListDrawable or maybe (probably) I'm missing something. 我对StateListDrawable有一个奇怪的问题,或者(可能)我缺少了一些东西。 I created a test application for it and the same problem occurs. 我为此创建了一个测试应用程序,并且发生了同样的问题。 So, this is my StateListDrawable resourse in file test_selection.xml 因此,这是文件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>

It's a very simple selector that draw a red color for selected state and a white rect for the unselected one. 这是一个非常简单的选择器,它为选中状态绘制红色,为未选中状态绘制白色矩形。

My main.xml template is very simple. 我的main.xml模板非常简单。 I simply use a TextView that uses the selection as background. 我只是使用将所选内容用作背景的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>

My Activity code is also very simple. 我的活动代码也非常简单。

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();
  };
 }
}

When Activity starts I try to set the state of my Drawable (the StateListDrawable) with the value SELECTED. 当Activity启动时,我尝试使用SELECTED值设置Drawable的状态(StateListDrawable)。 It seems all very simple.... but the problem is that the state is not shown. 看起来都很简单....但是问题是状态没有显示。 If, later, I click a button and execute the method updateView() the state changes. 如果稍后再单击一个按钮并执行方法updateView(),则状态会更改。 Where is my problem? 我的问题在哪里? Where am I wrong? 我哪里错了?

Thankx a lot Max 谢谢很多Max

Finally I found the solution. 终于我找到了解决方案。 It should be the same thing but it's not. 应该是同一回事,但事实并非如此。 I used this onCreate() version 我使用了这个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);
 }

Surprisingly calling setSelected(true) is different than calling setState() with int[]{android.R.attrs.state_selected} . 令人惊讶的是,调用setSelected(true)与使用int[]{android.R.attrs.state_selected}调用setState()有所不同。 This is due to internal View state. 这是由于内部View状态。

Bye Max 再见麦克斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM