我正在制作一个游戏,它将使用Listview
来将某些文本显示为聊天。Android - 更改ListView特定项目的背景颜色
其中一些文字代表事件,所以我想根据该特定事件更改背景颜色: EX。有人就会被杀死,那是特定Listview
项目将具有红色背景颜色
如何实现这一目标?
这是我的Java代码:
//STARTING SETUP
ArrayList<String> arrayListMother;
ArrayAdapter<String> arrayAdapterMother;
ListView listViewMother;
int counter;
boolean introDone = false;
String[] night = {"Welcome!","This is Lupus In Tabula!","You're gonna experience the Hasbro Game in a total new way, have fun!"};
String[] day = {"Thank you!","Great","Let's do it!"};
String[] dead = {"Thank you!","Great","Let's do it!"};
String[] saved = {"Thank you!","Great","Let's do it!"};
Button buttonGo;
//ENDING SETUP
//CREATE - CREATE//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
setup();
}
//CREATE - CREATE//
public void setup(){
arrayListMother = new ArrayList<String>();
arrayAdapterMother = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother);
listViewMother = (ListView) findViewById(R.id.list_mother);
listViewMother.setAdapter(arrayAdapterMother);
buttonGo = (Button)findViewById(R.id.buttonGo);
}
这是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list_mother"
android:text="@string/go"
android:layout_width="match_parent"
android:layout_height="0dp"
android:textAppearance="?attr/textAppearanceListItem"
android:divider="@null"
android:layout_weight="7"/>
<Button
android:id="@+id/buttonGo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/go"
android:textColor="@color/white"
android:background="@color/colorPrimary"
android:enabled="true"
android:onClick="play" />
</LinearLayout>
您可以用具有setter和getter模型类的颜色,与位置(列表项的指数) – Raghunandan
请问为例@Raghunandan – FET
,如果你发布你会得到一个更好的解决方案做到这一点很容易的代码第一。至少你尝试过的相关部分 – Raghunandan