[英]how to change the background color on activity with items in a listview?
我正在尝试在android studio中使用listview更改活动的背景颜色,但是当我单击listview中的项目时什么也没有发生。 有人能帮我吗?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.label,items));
view = this.getWindow().getDecorView();
view.setBackgroundResource(R.color.white);
}
public void onListItemClick(ListView parent, View v, char position, long id) {
String s = label.getText().toString();
double l = Double.parseDouble(s);
double o;
switch (position) {
case 0:
view.setBackgroundResource(R.color.red);
break;
case 1:
view.setBackgroundResource(R.color.green);
break;
case 2:
view.setBackgroundResource(R.color.white);
break;
case 3:
view.setBackgroundResource(R.color.orange);
break;
}
}
activity_main.xml
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
如下更新您的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_screen"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
并在您的活动中投放布局并将背景颜色应用于该布局。这将更改屏幕的背景颜色
private LinearLayout mLayout=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.label,items));
mLayout = (LinearLayout) findViewById(R.id.background_layout);
view = this.getWindow().getDecorView();
view.setBackgroundResource(R.color.white);
}
public void onListItemClick(ListView parent, View v, char position, long id) {
String s = label.getText().toString();
double l = Double.parseDouble(s);
double o;
switch (position) {
case 0:
mLayout .setBackgroundColor(Color.RED);
break;
case 1:
mLayout .setBackgroundColor(Color.GREEN);
break;
case 2:
mLayout.setBackgroundColor(Color.WHITE);
break;
case 3:
mLayout.setBackgroundColor(Color.parseColor("#FFA500"));
break;
}
}
从:
view.setBackgroundResource(R.color.red);
至:
view.setBackgroundColor(Color.RED);
推荐的方法是在项目文件中转到app/res/values
并打开colors.xml
在其中添加如下颜色: <color name="darkGreen">#669900</color>
然后在您的mainActivity.java中编写
Case0: view.setBackgroundColor(getResources().getColor(R.color.darkGreen)); break;
如果您需要找到所需颜色的十六进制数,则可以使用像这样的网站颜色选择器: https : //www.w3schools.com/colors/colors_picker.asp
在XML中,为父布局提供android:ID属性,并将其连接到Java类中。 然后onClick更改布局的颜色,它应该更改父布局的背景颜色。
这应该工作
首先将id添加到您的activity_main布局中的布局中,例如
<LinearLayout
android:id="@+id/LinearLayout1"
........
然后在您的活动中
final LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);
public void onListItemClick(ListView parent, View v, char position, long id) {
if(iscolor)
{
layout.setBackgroundColor(Color.BLUE);
iscolor = false;
}
else
{
layout.setBackgroundColor(Color.WHITE);
iscolor = true;
}
}
选择您正在使用的任何布局类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.