(安卓)如何填充ListView的背景和preserve头透明度透明度、背景、ListView、preserve

2023-09-06 02:32:47 作者:夏夜的琉璃

我有一个自定义图像背景填充整个屏幕后面一个ListView。

I have a custom image background that fills the entire screen behind a ListView.

的ListView中有一个包含一些数据,然后一个透明10dp余量(允许我们看到背景图像的一点点),然后再有更多数据的报头。下面这个头,在ListView的身体,我需要ListView的背景填充一路到屏幕的底部,即使项目内不走一路底部。

The ListView has a header that contains some data, then a transparent 10dp margin(allowing us to see a little bit of the background image), and then some more data. Below this header, in the "body" of the ListView, I need to make the ListView's background fill all the way to the bottom of the screen even if the items within do not go all the way to the bottom.

如果我设置的ListView的背景,然后我总是得到填充屏幕的底部,即使只有一对夫妇的物品(或根本没有)的预期效果。但是,这也掩盖了10dp透明分隔的头,让我再也无法看到自定义背景穿越的标题。

If I set the background of the ListView, then I get the desired effect of always filling to the bottom of the screen, even with only a couple items(or none at all). But this also covers up the 10dp transparent divider in the header, so that I can no longer see the custom background "through" the header.

有人可以请点我沿着正确的路径获得的两个的画面,同时保持头内的透明分隔填充?

Can someone please point me down the right path for getting both the filling of the screen while maintaining the transparent divider inside the header?

在传球,我可能需要创建假项目的细胞,可用于创建一个ListView背景的假象,而不会影响头部的透明分隔提到有人。不过,我希望能找到,如果可能的一个简单的解决方案..或者如果是的最佳解决方案是否有人可以给我就如何实现这一目标的最有效的方式一些指点。

Someone mentioned in passing that I might need to create "fake" item cells that could be used to create the illusion of a ListView background without impacting the header's transparent divider. However, I am hoping to find an easier solution if possible.. or if that is the best solution see if someone can give me some pointers on how to accomplish that in the most efficient way.

感谢您!

编辑:

以下是我定义的标题,虽然我觉得这个问题更多的是与ListView比这头,我正在通过addHeaderView()添加到ListView。

Here's how I am defining the header, although I think this question has more to do with the ListView than this header which I am adding to the ListView via addHeaderView().

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@android:color/transparent" android:orientation="vertical">

    <include android:id="@+view_list_header/headertop"
        layout="@layout/view_list_item"/>

    **<LinearLayout android:id="@+view_list_header/ll_transparent_filler"
    android:layout_width="fill_parent" android:layout_height="10dp"/>**     

    <RelativeLayout android:id="@+view_list_header/sort" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView.../> <ImageView.../> <TextView.../>
    </RelativeLayout>
</RelativeLayout>

于是,我试着创建10dp填充物,目前我使用的是空10dp的LinearLayout,这样我可以得到它的一个引用的各种方式,并设置其知名度里去了时,我想(我我不是在做我讨论的情况下)

So, I've tried various ways of creating the 10dp filler, currently I'm using an "empty" 10dp LinearLayout so that I can obtain a reference to it and set its visibility to GONE when I want to(which I am NOT doing in the case I am discussing)

我设置ListView的背景是这样的:listView.setBackgroundResource(资源)

I am setting the ListView's background like this: listView.setBackgroundResource(resource).

此外,当我做背景设置为这个ListView中,我得到的背景期望的行为总是充满整个屏幕,即使我已经添加项目到列表的数目(顺便说一句都使用相同的背景资源那我设置到ListView)是不够的,充满整个屏幕。但是,当我这样做,10dp透明保证金的标题是不可见的,因为,它的背后,则列表视图背景是$ P $,从被视为pventing的基本观点。

Again, when I do set the background to this ListView, I get the desired behavior of the background always filling the entire screen, even if the number of items I have added to the list(which incidentally all use the same background resource that I am setting to the ListView) is not enough to fill the screen. But when I do this, the 10dp "transparent margin" in the header is not visible because, behind it, the ListViews background is preventing the underlying views from being seen.

我假定这是因为ListView的背景也去背​​后的页眉和页脚的看法。

的一点是,有很多的方法来创建的元素,让底层的视图来显示通过之间的10dp透明保证金。我的问题在于创造一个背景在 ListView的身体(即不是页眉/页脚)总是充满屏幕,即使在名单部分填充或空和与此同时preserving,在头透明度,让我仍然可以看到有意见后面的ListView。

The point is, there are lots of ways to create a 10dp "transparent margin" between elements that will allow the underlying views to show through. My problem lies in creating a background for the ListView "body"(i.e. not the Header/Footer) that always fills the screen, even when the list is partially filled or empty and at the same time preserving that transparency in the header so that I can still see the views that are "behind" the ListView.

推荐答案

我加入了彩色的页脚砍死它。页脚是含有一种叫做视图中的LinearLayout补设置为5DP高度瓦特/我想要的背景颜色。

I hacked it by adding a colored footer. The footer was a LinearLayout containing a View called "fill" set to 5dp height w/ my desired bg color.

填充和放后;扩展我的表头(前code),然后我调整了页脚填补剩余的屏幕空间:

After populating & scaling my list header (prior code), I then adjusted the footer to fill in the remaining screen space:

        ...

    int totHeight = getTotalHeightofListView();

    if((mHeader.getMeasuredHeight() + totHeight) < mList.getMeasuredHeight())
        addFooterFill(mHeader.getMeasuredHeight() + totHeight);
}

protected int getTotalHeightofListView() {

    ListAdapter LvAdapter = mList.getAdapter();
    int totHeight = 0;
    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, mList);
        mView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        totHeight += mView.getMeasuredHeight();
    }

    return totHeight; 
}

protected void addFooterFill(int sizeUsed){

    View fill = (View) mFooter.findViewById(R.id.fill);

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) fill.getLayoutParams();
    params.height = mList.getMeasuredHeight() - sizeUsed;
    fill.setLayoutParams(params);
}

在理论上,如果你已经有了一个页脚这应该工作甚。你本质上只是调整页脚中的视图的高度。

In theory this should work even if you already have a footer. You're essentially just adjusting the height of a view within the footer.