【发布时间】:2016-01-10 14:33:51
【问题描述】:
我正在制作一个游戏,它将使用 Listview 来显示一些文本作为聊天。
其中一些文本代表事件,所以我想根据该特定事件更改它们的背景颜色:
前任。有人被杀,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 以及位置(listitem 的索引)的模型类轻松做到这一点
-
我可以问一个例子@Raghunandan
-
如果您先发布代码,您将获得更好的解决方案。至少您尝试过的相关部分
-
当然,编辑@Raghunandan
-
不工作@Raghunandan
标签: android listview android-listview