Android - 更改 ListView 特定项目的背景颜色
Android - Change background color of specific item of ListView
我正在制作一个游戏,它将使用 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>
使用此代码:
listViewMother.getChildAt(position).setBackgroundColor(
Color.parseColor("#00743D"));
为 ArrayAdapter
覆盖 getView
。根据条件更改颜色或执行所需操作。您已经为 ArrayAdapter
构造函数提供了参数。所以不需要再次膨胀视图。得到相同的并做所需要的。同样getItem(position)
获取指定位置的数据。
http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)
listViewMother.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
if(getItem(position).equals("Somebody gets killed"))
{
// do something change color
row.setBackgroundColor (Color.RED); // some color
}
else
{
// default state
row.setBackgroundColor (Color.WHITE); // default coloe
}
return row;
}
});
在绑定视图的适配器 class 中,您可以检查特定条件并按照以下方式设置颜色 way.This 您可以动态设置文本或背景的颜色.希望这可以帮助。
我在这里使用回收器查看器,它覆盖了 onBindViewHolder。你的适配器应该有绑定视图的方法。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Person person = personList.get(position);
holder.username.setText(person.getName());
holder.arriveTime.setText(DateFormat.format("hh:mm a",(person.getArriveTime() * 1000)));
if(person.getName().equals("NoVisitor"))
{
holder.username.setTextColor(Color.GRAY);
holder.arriveTime.setTextColor(Color.GRAY);
holder.leaveTime.setTextColor(Color.GRAY);
}
else
{
holder.username.setTextColor(Color.BLACK);
holder.arriveTime.setTextColor(Color.BLACK);
holder.leaveTime.setTextColor(Color.BLACK);
}
}
我正在制作一个游戏,它将使用 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>
使用此代码:
listViewMother.getChildAt(position).setBackgroundColor(
Color.parseColor("#00743D"));
为 ArrayAdapter
覆盖 getView
。根据条件更改颜色或执行所需操作。您已经为 ArrayAdapter
构造函数提供了参数。所以不需要再次膨胀视图。得到相同的并做所需要的。同样getItem(position)
获取指定位置的数据。
http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)
listViewMother.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
if(getItem(position).equals("Somebody gets killed"))
{
// do something change color
row.setBackgroundColor (Color.RED); // some color
}
else
{
// default state
row.setBackgroundColor (Color.WHITE); // default coloe
}
return row;
}
});
在绑定视图的适配器 class 中,您可以检查特定条件并按照以下方式设置颜色 way.This 您可以动态设置文本或背景的颜色.希望这可以帮助。 我在这里使用回收器查看器,它覆盖了 onBindViewHolder。你的适配器应该有绑定视图的方法。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Person person = personList.get(position);
holder.username.setText(person.getName());
holder.arriveTime.setText(DateFormat.format("hh:mm a",(person.getArriveTime() * 1000)));
if(person.getName().equals("NoVisitor"))
{
holder.username.setTextColor(Color.GRAY);
holder.arriveTime.setTextColor(Color.GRAY);
holder.leaveTime.setTextColor(Color.GRAY);
}
else
{
holder.username.setTextColor(Color.BLACK);
holder.arriveTime.setTextColor(Color.BLACK);
holder.leaveTime.setTextColor(Color.BLACK);
}
}