java - 如何更改 ListView 中项目的 Activity 背景颜色?

标签 java android listview

我正在尝试使用 android studio 中的 ListView 更改 Activity 的背景颜色,但是当我单击 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>

并在您的 Activity 中转换布局并将背景颜色应用于布局。这将更改屏幕的背景颜色

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;
        }
    }

关于java - 如何更改 ListView 中项目的 Activity 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47458610/

相关文章:

java - 如何在Java中存储超过10位的数字

Java 代码清理器

java - 为什么 java Grep 会因 OutOfMemoryError 崩溃?

java - Android Webview 页面无法正确加载页面

android - 无法在 SAMSUNG GALAXY S7 EDGE 上调试

android - 崩溃:android.widget.ListView.layoutChildren 中的 java.lang.IllegalStateException

java - 图像归一化给出黑色图像

java - 如何在我的应用程序中使用-assumenosideeffects class android.util.Log

android - 动态地从数据库加载数据到 ListView 中

Android:如何对齐底部的按钮和上方的 ListView ?