我正在製作一個遊戲,它將使用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