Android ListView 填充内容而不移动 ListView 高度?

发布于 2024-12-11 19:54:15 字数 226 浏览 0 评论 0原文

我有一个 ListView,里面有一堆项目。如何使顶部和底部项目的顶部边距为 10dp,底部项目的底部边距为 10dp?现在我可以通过 ListView 上的填充或边距来做到这一点,但结果是,当您滚动时,ListView 的边缘现在距屏幕底部 10dp。无论如何?我还尝试在适配器的 getView 方法内部设置边距,但我没有看到 AbsListView.LayoutParams 的任何边距选项。任何帮助都会很棒。

谢谢

I have a ListView with a bunch of items inside it. How do I make the top and bottom items have a top margin of 10dp for top item and bottom margin of 10dp for bottom item? Now I can do this with padding or margin on the ListView but the result is that when you scroll the edge of the ListView is now 10dp from the bottom of the screen. Anyway around this? I have also tried setting margins inside of the getView method of my adapter but I don't see any margin options for AbsListView.LayoutParams. Any help would be great.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-12-18 19:54:15

诀窍是在视图定义中包含 android:clipToPadding="false" ,例如:

<ListView android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dip"
    android:paddingBottom="16dip"
    android:clipToPadding="false" />

The trick is to include android:clipToPadding="false" in your view definition, e.g.:

<ListView android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dip"
    android:paddingBottom="16dip"
    android:clipToPadding="false" />
请恋爱 2024-12-18 19:54:15

您可以通过使用页眉和页脚在滚动区域内的 ListView 顶部和底部获得空间。这是一个简单的示例:

定义您的填充视图,我将其命名为 header_footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="10dp"/>

现在,您的 onCreate 方法如下所示,假设您将 ListView 命名为 listView

    final LayoutInflater inflater = 
        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View headerFooter = inflater.inflate(R.layout.header_footer, null);
    listView.addFooterView(headerFooter);
    listView.addHeaderView(headerFooter);

然后设置您的适配器。

You can get space at the top and bottom of your ListView, inside the scrolling region, by using headers and footers. Here's a quick example:

Define your filler view, which I named header_footer.xml:

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="10dp"/>

Now your onCreate method looks like this, assuming you named your ListView listView:

    final LayoutInflater inflater = 
        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View headerFooter = inflater.inflate(R.layout.header_footer, null);
    listView.addFooterView(headerFooter);
    listView.addHeaderView(headerFooter);

And then set your adapter.

凯凯我们等你回来 2024-12-18 19:54:15

为什么不只检查项目相对于列表大小的位置:

填充:

 public View getView(int position, View convertView, ViewGroup parent)
    {
         //recycle views and whatever else you normally would do goes here..
         //...
         //...

         if (position == 0){
            convertView.setPadding(0, 10, 0, 0); //padding on top for top item
         }
         else if (position == getCount() - 1){
            convertView.setPadding(0, 0, 0, 10); //padding on bottom for bottom item
         }
         else{
            convertView.setPadding(0, 0, 0, 0); //no padding
         }
}

对于边距,请使用

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
convertView.setLayoutParams(lp);

示例:

public View getView(int position, View convertView, ViewGroup parent)
    {
             //recycle views and whatever else you normally would do goes here..
             //...
             //...
             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,        LinearLayout.LayoutParams.WRAP_CONTENT);
             if (position == 0){
                 lp.setMargins(0, 10, 0, 0); //margin on top for top item
             }
             else if (position == getCount() - 1){
                lp.setMargins(0, 10, 0, 10); //margin on bottom for bottom item
             }
             else{
                lp.setMargins(0, 0, 0, 0); //no margin
             }
             convertView.setLayoutParams(lp);
    }

前提是您已为适配器正确实现了 getCount() 方法。

Why not just check the position of the item relative to the size of your list:

Padding:

 public View getView(int position, View convertView, ViewGroup parent)
    {
         //recycle views and whatever else you normally would do goes here..
         //...
         //...

         if (position == 0){
            convertView.setPadding(0, 10, 0, 0); //padding on top for top item
         }
         else if (position == getCount() - 1){
            convertView.setPadding(0, 0, 0, 10); //padding on bottom for bottom item
         }
         else{
            convertView.setPadding(0, 0, 0, 0); //no padding
         }
}

For margins use

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
convertView.setLayoutParams(lp);

Ex:

public View getView(int position, View convertView, ViewGroup parent)
    {
             //recycle views and whatever else you normally would do goes here..
             //...
             //...
             LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,        LinearLayout.LayoutParams.WRAP_CONTENT);
             if (position == 0){
                 lp.setMargins(0, 10, 0, 0); //margin on top for top item
             }
             else if (position == getCount() - 1){
                lp.setMargins(0, 10, 0, 10); //margin on bottom for bottom item
             }
             else{
                lp.setMargins(0, 0, 0, 0); //no margin
             }
             convertView.setLayoutParams(lp);
    }

this is provided you've implemented the getCount() method properly for your adapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文