首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓刷新StateListDrawable问题

安卓刷新StateListDrawable问题
EN

Stack Overflow用户
提问于 2010-08-25 15:57:32
回答 1查看 3.7K关注 0票数 1

我对StateListDrawable有一个奇怪的问题,或者(可能)我遗漏了什么。我为它创建了一个测试应用程序,同样的问题也出现了。这是我在文件test_selection.xml中的StateListDrawable资源

代码语言:javascript
复制
<?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。

代码语言:javascript
复制
<?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>

我的活动代码也非常简单。

代码语言:javascript
复制
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();
  };
 }
}

当活动开始时,我尝试用选定的值设置我的可绘制( StateListDrawable)的状态。看起来一切都很简单...但问题是没有显示状态。如果稍后我单击一个按钮并执行updateView()方法,状态就会改变。我的问题在哪里?我哪里错了?

谢谢你,麦克斯

EN

回答 1

Stack Overflow用户

发布于 2010-08-26 21:40:17

最后我找到了解决方案。这应该是一回事,但事实并非如此。我使用的是这个onCreate()版本

代码语言:javascript
复制
/** 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)与使用int[]{android.R.attrs.state_selected}调用setState()是不同的。这是由于内部视图状态造成的。

再见,麦克斯

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3563902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档