【问题标题】:ListView Background列表视图背景
【发布时间】:2013-06-11 11:22:41
【问题描述】:

已编辑:

我在为我的 ListView 设置背景图像时遇到问题。在活动的图形视图中,它看起来不错,但在手机上,根本没有背景。这是我的活动 .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"  
android:gravity="bottom|fill_vertical" 
android:background="@drawable/background">

   <ListView
      android:id="@+id/listPlan"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:divider="@null"
      android:cacheColorHint="#00000000" 
      android:listSelector="@android:color/transparent" 
      android:scrollingCache="false" 
      android:dividerHeight="5dp" >

   </ListView>

</LinearLayout>

如果它对我的问题有意义的话,它就是 GroupActivity 中的一项活动。我已经尝试了所有方法 - 为 ListView 提供 android:background,为 LinearLayout,甚至为父活动 (GroupActivity),为每个项目更改 android:layout 值,没有任何帮助。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/text"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" 
     android:textSize="20sp"
     android:padding="15dp" 
     android:scrollHorizontally="true" >
</TextView>

这是我的 Single List Item 的样子,可能有一些错误?

只有当我编辑我的&lt;TextView&gt; 并将其提供给android:background 时,我才看到差异,每个人都尝试更改背景,因为这个列表视图没有被发现 - 例如,我已将 android:cacheColorHint="#00000000" 更改为 android:cacheColorHint="#FFFFFFFF" - 没有发生任何事情.

我会再试一次 - 在将布局从线性更改为相对之后什么也没发生,但这(感谢@Sam)让我思考。也许我必须在我的 AndroidManifest.xml 中添加一些东西?如果代码正常工作,也许这个父GroupActivityListView 有更大的影响,然后我想。 不幸的是我不能放图片,但你可以在这里看到ListView 的两种状态: https://www.dropbox.com/sh/y5dgibei5uq9kv9/afeIvgW18g

哇,问题有点奇怪,因为当我尝试为我的ListView 设置android:textColor 时,没有任何改变! 这是我的styles.xml:

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

</resources>

感谢您的每一个回复。

最后一次编辑!

好的,问题解决了,因为没有任何帮助,我尝试了另一种方法,只是在代码中设置了我的 ListView 背景:

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        list.setBackgroundResource(R.drawable.background);
    else list.setBackgroundResource(R.drawable.background_landscape);

现在一切正常!谢谢大家的每一个回复。和平相处。

【问题讨论】:

  • 您在列表中有项目?因为它可能会扭曲内容?
  • 是的,我在列表中有项目。
  • 我什至尝试将 ImageView 放在 ListView 下并在 cacheColorHint try 之后,但仍然没有。
  • 请检查我编辑的答案../

标签: android xml listview background textview


【解决方案1】:

这解决了我的问题:-

android:cacheColorHint="#0000"

将其添加到您的列表视图中:-

<ListView
android:id="@+id/listPlan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:cacheColorHint="#0000">

"#0000" -> 透明色码

【讨论】:

    【解决方案2】:

    试试这个:

          <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/contract_diary_list"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"        
           android:fadingEdge="none"        
           android:divider="@null"
           android:scrollingCache="false"
          android:listSelector="@android:color/transparent"
         >
         </ListView> 
    
       </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      只需将其添加到 listview.xml 文件中

      android:cacheColorHint="#00000000"
      

      或者你可以使用Expandable Listview
      已编辑:

      使用Relative layout 代替linear layout 请检查../

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      
          <ListView
              android:id="@+id/listView1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true"
              android:background="@drawable/ic_launcher" 
              android:cacheColorHint="#00000000">
          </ListView>
      
      </RelativeLayout>
      

      【讨论】:

      • 不幸的是它对我没有帮助。我也尝试将android:background 分配给RelativeLayout,但是没有任何改变。 :(
      • 就像ListView 没有响应我的电话。
      • 可能是上帝知道问题出在哪里......如果你解决了请告诉我们。
      猜你喜欢
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      相关资源
      最近更新 更多