Android 2.3.3 上的列表视图页脚背景

Posted

技术标签:

【中文标题】Android 2.3.3 上的列表视图页脚背景【英文标题】:List View Footer Background on Android 2.3.3 【发布时间】:2011-09-06 17:41:29 【问题描述】:

这个很奇怪。我有一个列表视图,它是相对布局的一部分。我已经为此相对布局设置了背景,并将列表视图背景设置为透明。

现在,直到今天早上一切都很好。即使我的列表视图中只有一行,我也可以看到整个屏幕都被我的自定义背景覆盖。

然后,我获得了 Verizon Motorola Droid X 的 2.3.3 更新(之前是 2.2)。更新后,我再次启动了我的应用程序,现在会发生这种情况。

如果我的列表视图只有一行,我会在其下方看到一个白色区域,而不是我的自定义背景。 但是如果它有 100 行并因此覆盖了整个屏幕,我就不会看到那个奇怪的白色背景。我的相对布局将宽度和高度设置为“fill_parent”。

我已在底部发布了我的 XML。有没有其他人遇到过这个问题,或者我犯了一些非常愚蠢的错误。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background = "@drawable/background" 
    android:layout_
    android:layout_>
    <ListView   android:id = "@+id/listQueue"
                android:layout_width = "fill_parent"
                android:layout_height = "fill_parent"
                android:layout_below = "@id/homeScreenBanner"
                android:background="@android:color/transparent"
                android:divider="@drawable/separator"
                android:scrollingCache="false"/>
</RelativeLayout>

编辑:

我想我已经找到了解决这个问题的方法:

将 layout_height 属性更改为 wrap_content,它就像一个魅力。 :)

跟随改变的路线。 android:layout_height = "wrap_content"

【问题讨论】:

嗯,2.3 的主题发生了很大变化。也许摩托罗拉对其皮肤进行了定制,这与您的假设相冲突?尝试使用 base 2.3 创建一个模拟器并在那里进行测试。 我想我已经找到了问题的答案。至少它为我解决了问题。对于我的列表视图,我进行了以下更改: android:layout_height = "wrap_content" 为什么是android:layout_below = "@id/homeScreenBanner"?您的 homeScreenBanner 在哪里?...您只需删除它并尝试.. 如果您想将背景颜色设置为黑色以外的颜色,则将 layout_height 更改为 wrap_content 将不起作用。这看起来像是摩托罗拉 2.3.3 端口中的一个错误。我在 Droid 3 上看到了同样的问题。 这里是另一个有同样问题的帖子。 ***.com/questions/6508354/… 【参考方案1】:

我编写了一个可以在 Android 2.1/2 中使用的类,它将在 2.3 中使用 ListViews 进行反射:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ListView;

public class TransparentListView extends ListView 

    private void makeTransparent() 
        if (Build.VERSION.SDK_INT >= 9) 
            try 

                Method overscrollFooterMethod = 
                    TransparentListView.class.getMethod("setOverscrollFooter", new Class[] Drawable.class);
                Method overscrollHeaderMethod = 
                    TransparentListView.class.getMethod("setOverscrollHeader", new Class[] Drawable.class);


                try 
                    overscrollFooterMethod.invoke(this, new Object[] null);
                    overscrollHeaderMethod.invoke(this, new Object[] null);
                 catch (IllegalArgumentException e) 
                    e.printStackTrace();
                 catch (IllegalAccessException e) 
                    e.printStackTrace();
                 catch (InvocationTargetException e) 
                    e.printStackTrace();
                
             catch (SecurityException e) 
                e.printStackTrace();
             catch (NoSuchMethodException e) 
                e.printStackTrace();
            
        
    

    public TransparentListView(Context context) 
        super(context);
        this.makeTransparent();
    

    public TransparentListView(Context context, AttributeSet attrs) 
        super(context, attrs);
        this.makeTransparent();
    

    public TransparentListView(Context context, AttributeSet attrs, int defStyle) 
        super(context, attrs, defStyle);
        this.makeTransparent();
    

在 XML 中使用如下:

<com.myapp.TransparentListView android:id="@android:id/list"
    android:layout_
    android:layout_
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1"
    android:dividerHeight="0dip"
    android:divider="#00000000"
    android:cacheColorHint="#00000000"
/>  

【讨论】:

完美! Google 的开发人员应该将其 c/p 到他们的源代码中。非常感谢!【参考方案2】:

使用完全在 .xml 布局文件中实现的基本布局实际上有一个简单的解决方案。

首先,摩托罗拉博客的建议是不可接受的,因为它需要构建到 API v2.3 才能使用“android:overScrollFooter”强制多个构建或大多数 Android 设备无法访问。

设置 ListView 的 android:layout_height="wrap_content" 的建议在很多情况下都有效,如果 ListView 下方没有布局项,该建议非常有用。

当ListView 有一个或多个View 的上方和一个或多个View 的下方时,会出现此问题。这需要为 ListView 设置 android:layout_belowandroid:layout_above,这会将 ListView 的高度拉伸到大于必要的“wrap_content”的可用空间高度。发生这种情况时,“Bug”就会出现 - ListView 底部的额外空间被页脚的默认背景颜色填充,该背景颜色为深灰色且不透明。

现在是简单的解决方案

将 ListView 放置在垂直 LinearLayout 或等效的内部。使用height="fill_parent" 允许LinearLayout 从可用空间的顶部到底部跨越,多余的部分将填充正确的背景可绘制对象或颜色。将 ListView 的高度设置为"wrap_content",这样难看的灰色页脚背景就没有未使用的空间。

这是一个例子:

<Button> android:id=@+id/MyTopButton" ... </Button>
<Button> android:id=@+id/MyBottomButton" android:layout_alignParentBottom="true" ... </Button>

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:layout_below="@+id/MyTopButton"
    android:layout_above="@+id/MyBottomButton"
    >

    <!-- Note: This is a bug fix for a particular problem experienced on 
         Motorola OS v2.3.3 devices only in which the space below the unused 
         portion of the ListView is rendered in gray instead of the desired 
         transparent 'cacheColorHint' color. This solution which wraps the 
         ListView in another container (a LinearLayout in this case) allows 
         the ListView to be of height "wrap_content" while the parent 
         container is forced to be the desired height - namely from the 
         header area above down to the buttons below. -->

    <ListView
        android:layout_
        android:layout_
        android:id="@+id/MyList"
        android:cacheColorHint="#00000000"
        />

</LinearLayout>

【讨论】:

向自定义列表视图项添加填充,但此操作失败,填充将转换为底部丑陋的灰色条【参考方案3】:

是的,David 的解决方案在某些情况下有效,并允许您使用 2.3 之前的 Android 进行构建。事实上,这是我在 6 月份发布在 MotoDev forum 上的第一个解决方案,带有可下载的代码示例。

但是,一些开发人员说它会留下一个伪影(意思是:列表底部有一条杂散的水平灰线)。我自己见过这个。这对于那些关心设计细节的人来说是行不通的。 android:overScrollFooter="@null" 解决方案不会留下工件。但你是对的,要使用它,你需要使用 Android 2.3 或更高版本构建。

【讨论】:

【参考方案4】:

请参阅摩托罗拉的blog post 了解此行为 - 答案是,在 Motoblur 2.3 上,他们设置了默认的过度滚动页脚,可以通过设置 android:overScrollFooter="@null" 进行修复,但仅适用于 Android 2.3+。

他们推荐的 appwide 解决方案是使用自定义主题和自定义 listViewStyle 集,仅在具有该属性集的 values-v10 文件夹中设置。

【讨论】:

不确定摩托罗拉的方法是否有效。如果我的项目为 Android 1.6 进行编译,那么 values-v10 仍会在构建时进行评估并出现 XML 错误:XML 文件中的错误:正在中止构建。 您可能需要使用 10 的目标进行编译。min-sdk 仍然可以更低,并且旧版本应该永远不会看到 values-v10 文件夹中的值,所以一切应该仍然有效;您只需要注意目标 SDK 高于 min-sdk 的事实,并注意不要在其他地方使用比您的 min-sdk 更高的 SDK 版本中引入的属性或 API 调用。 +Yoni 我确实尝试过,但它仍然给了我一个错误。我不确定为什么。但我继续使用 LinearLayout 实现了上面显示的技术,效果很好。【参考方案5】:

您可以尝试将 ListView 的 cacheColorHint 设置为透明吗?

android:cacheColorHint="@android:color/transparent"

【讨论】:

以上是关于Android 2.3.3 上的列表视图页脚背景的主要内容,如果未能解决你的问题,请参考以下文章

android listviews:页眉和页脚视图

Android列表视图将内容包装到基于页脚大小的特定大小

Android底页上的静态页脚视图

Android底表上的静态页脚视图

通过单击列表视图上的列表项更改 MapView Android Studio 上的位置相机视图

列表视图上的 Android 联系人