【问题标题】:android listactivity background colorandroid listactivity背景颜色
【发布时间】:2023-04-03 09:33:01
【问题描述】:

如何设置列表活动的背景颜色? 我可以设置列表视图项目的背景颜色,但不能设置孔 Activity-View(参见图像底部的黑色部分)。我怎样才能做到这一点?

ListActivity XML 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="@color/darkbluelogo" >

    <ImageView android:id="@+id/list_image"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:contentDescription="@id/list_image"
         />
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp" 
        android:background="@color/darkbluelogo"
        android:scrollingCache="false" 
        android:cacheColorHint="#00000000" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+id/title" >
        </TextView>

        <TextView
            android:id="@+id/datetime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+id/datetime" >
        </TextView>

    </LinearLayout>
</LinearLayout>

这就是它在设备上的显示方式:

解决方案:

我必须添加样式 XML 并将其添加到 AndroidManifest.xml 中的活动中

这是正确的答案: https://stackoverflow.com/a/10426474/1306012

【问题讨论】:

  • 尝试让列表背景颜色透明,线性布局darkbluelogo
  • 感谢您的回复。如何设置列表背景颜色?
  • 您可以在 XML 中执行(但我认为您在那里没有列表)或在 JAVA 中使用list.setBackgroundColor(color);

标签: android background-color listactivity


【解决方案1】:

在包含您的列表视图的 xml 文件中将 layout_height 设置为 fill_parent。并根据需要设置该 xml 文件的背景。

编辑 :在这种情况下,您可以使用主题来设置背景

喜欢这个

<style name="MyTheme">

    <item name="android:background">@color/darkbluelogo</item>

</style>

并将此主题应用于清单文件中的列表活动。

    <activity
        android:theme="MyTheme"

【讨论】:

  • 感谢您的回复,但我的情况似乎没有效果。
  • @viperbone 您使用哪种适配器来填充您的列表视图?包含 ListView 的 XML 文件在哪里?
【解决方案2】:

以下代码展示了如何设置 ListActivity 的背景颜色。

getListView().setCacheColorHint(Color.rgb(36, 33, 32));
getListView().setBackgroundColor(Color.rgb(36, 33, 32));

【讨论】:

  • 抱歉这对我的情况没有影响?
【解决方案3】:

你已经给出了你的主要线性布局的高度

 android:layout_height="wrap_content"

做起来

android:layout_height="fill_parent"

它会改变整个背景的颜色。

【讨论】:

  • 感谢您的回复,但我的情况似乎没有效果。
【解决方案4】:

嗨,如果您使用了默认的 android 列表视图,请添加以下内容:

  setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, CONTENT));

    final ListView listView = getListView();
    listView.setBackgroundColor(Color.parseColor("#4597CD"));

或者如果你在 xml 布局中设置了列表视图,那么:

<ListView
    android:id="@+id/listviewID"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#4597CD"
    android:cacheColorHint="@color/darkbluelogo"
  />

【讨论】:

    【解决方案5】:

    检查 ListView 属性的宽度和高度都应该是“fill_parent”..

     <ListView
        android:id="@+id/listCategory"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/darkbluelogo"
        android:cacheColorHint="@color/darkbluelogo"
      />
    

    【讨论】:

      【解决方案6】:
      super.getListView().setBackgroundColor(getResources().getColor(R.color.red));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-22
        • 2012-06-10
        • 2019-11-10
        • 2015-08-13
        • 2015-07-05
        • 2013-08-06
        • 2013-08-04
        • 2023-03-06
        相关资源
        最近更新 更多