我在将一些有效的 XML 文件转换为代码时遇到了一些问题。 我有一个 ListView,我需要能够在运行时从高级未知的资源切换其按下/选中的可绘制对象(因此为什么不使用 XML);
以下配置效果很好:
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" >
</ListView>
</LinearLayout>
选择器.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue" android:state_pressed="true"/>
<item android:drawable="@drawable/green" android:state_checked="true"/>
<item android:drawable="@drawable/orange"/>
</selector>
list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:padding="10dp" />
主要.java:
public class Main extends Activity {
StateListDrawable selector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
StringAdapter adapter = new StringAdapter(this, R.layout.list_row);
adapter.add("one"); adapter.add("two"); adapter.add("three"); adapter.add("four");
adapter.add("five"); adapter.add("six"); adapter.add("seven"); adapter.add("eight");
adapter.add("nine"); adapter.add("ten"); adapter.add("eleven"); adapter.add("twelve");
listView1.setAdapter(adapter);
}
private class StringAdapter extends ArrayAdapter<String>{
public StringAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CheckedTextView tv = (CheckedTextView ) inflater.inflate(R.layout.list_row, parent, false);
tv.setText(getItem(position));
return tv;
}
}
}
结果是这样的,很棒(pressed:blue, checked:green, else:orange):
但是,如果我删除
android:background="@drawable/selector
来自 list_row_xml,并通过代码应用它:
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable green = getResources().getDrawable(R.drawable.green);
Drawable orange = getResources().getDrawable(R.drawable.orange);
selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, blue);
selector.addState(new int[] { android.R.attr.state_checked }, green);
selector.addState(new int[] { }, orange);
tv.setBackgroundDrawable(selector);
我得到以下信息(所有东西都得到 state_pressed 可绘制对象,蓝色):
出了什么问题?我很确定我已将选择器转换为适当的代码。
最佳答案
我复制了你的代码,一切正常。
不过,您应该确保在 getView()
中创建一个新的 selector
实例。否则,如果您对所有项目使用相同的选择器
,则按一个项目将影响您的所有项目。
这是您的 getView()
方法的样子
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CheckedTextView tv = (CheckedTextView) inflater.inflate(R.layout.list_row, parent, false);
tv.setText(getItem(position));
Drawable blue = getResources().getDrawable(R.drawable.blue);
Drawable green = getResources().getDrawable(R.drawable.green);
Drawable orange = getResources().getDrawable(R.drawable.orange);
selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, blue);
selector.addState(new int[] { android.R.attr.state_checked }, green);
selector.addState(new int[] {}, orange);
tv.setBackgroundDrawable(selector);
return tv;
}
当然,您可以进行一些优化,但这会奏效。
关于android - 从代码而非 xml 创建选择器时出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158838/