简体   繁体   English

如何在列表视图的顶部和底部对列表项的角进行舍入? (不仅适用于顶部和底部物品)

[英]How to round corners of list item at the top and bottom of the list view? (not only for top and bottom items)

I have a listview with rounded corner at the top and the bottom. 我有一个顶部和底部有圆角的列表视图。 Look like this image: 看起来像这个图像: 在此输入图像描述

But when I click on the top and the bottom of ListView, the List Item's background is rectangle not rounded as background of ListView at top and bottom. 但是当我单击ListView的顶部和底部时,List Item的背景是矩形而不是圆形作为ListView的顶部和底部背景。 Like this image : 喜欢这个图片:

列表项的角不圆

How to resolve this issue? 如何解决这个问题?

this is my code : 这是我的代码:

1/ list_activity.xml 1 / list_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cacaca">

<ListView
    android:id="@+id/listView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/list_border"
    android:listSelector="@drawable/list_selector"/>
</LinearLayout>

2/ list_border.xml 2 / list_border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:bottomRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"/>
</shape>

3/ list_selector.xml 3 / list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@color/colorPrimary" />
</selector>

4/ ListActivity.java 4 / ListActivity.java

ListView listView = (ListView)findViewById(R.id.listView3);

ArrayList<String> listItems = new ArrayList<>();

for(int i=0;i<20;i++){
    listItems.add(""+i);
}

ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_selectable_list_item, listItems);
listView.setAdapter(adapter);

This solution is not pretty but it does what you want. 这个解决方案并不漂亮,但它可以满足您的需求。 The idea is to invert the corner and draw them as foreground : 想法是反转角落并将它们绘制为前景:

the ListView is wrapped in a FrameLayout : ListView包装在FrameLayout

<FrameLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_margin="10dp"
      android:foreground="@drawable/inverted_corners"
      android:layout_weight="1">
      <ListView
          android:id="@+id/listView3"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#fff"
          android:listSelector="@drawable/list_selector" />
  </FrameLayout>

which foreground ( drawable/inverted_corners.xml ) is set to a rounded hole , drawn with the background color. 将前景( drawable/inverted_corners.xml )设置为圆孔 ,使用背景颜色绘制。 The trick is to draw a line outside the shape, instead of a filled shape : 诀窍是在形状外画一条线 ,而不是填充形状:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="-11dp"
        android:left="-11dp"
        android:right="-11dp"
        android:top="-11dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="10dp"
                android:color="#cacaca" />
            <corners android:radius="22dp" />
        </shape>
    </item>
</layer-list>

This garanties that the rounded corners are on top of the selector, and the overscroll effect. 这可以确保圆角位于选择器顶部,并具有过卷效果。

I suggest you should set the layout with corners to your ListView 's first and last element, but not to the whole ListView . 我建议你应该为你的ListView的第一个和最后一个元素设置带角的布局,而不是整个ListView

You can create two extra .xml files in addition to your list_selector.xml : one for first element with the round corners at the top (lets call it list_selector_top.xml ) and another one for the last element with the round corners at the bottom ( list_selector_bottom.xml ). 除了list_selector.xml之外,您还可以创建两个额外的.xml文件:一个用于第一个元素,顶部有圆角(让我们称之为list_selector_top.xml ),另一个用于最后一个元素,底部有圆角( list_selector_bottom.xml )。

Then you can create your own MyAdapter extends ArrayAdapter and somewhere in getView() method set the appropriate background to your element's View (if position==0 set list_selector_top.xml , if position==getCount()-1 set list_selector_bottom.xml , by default set list_selector.xml ). 然后你可以创建自己的MyAdapter extends ArrayAdapter并在getView()方法的某个地方为你的元素的View设置适当的背景(如果position==0 set list_selector_top.xml ,if position==getCount()-1 set list_selector_bottom.xml ,by default set list_selector.xml )。

It might be not the simpliest approach, but it works. 它可能不是最简单的方法,但它有效。

Set your corner radius to list view selector also using this: 将您的角半径设置为list view选择器也使用此:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/list"
        android:state_pressed="true"
        >
        <shape>
        <solid android:color="@color/colorPrimary"/>
        <corners
            android:radius="10dp"/>
        </shape>
        </item>

</selector>

so your selection will not be cross boundry of list view . 所以你的选择不会是list view交叉边界。

在此输入图像描述

cons - you will not able to define which radius you have to set as per top and bottom part that is why have to set radius to all sides. 缺点 -你无法确定哪些radius你必须设置为每顶部和底部,这就是为什么要设置radius到四面八方。

hope this should help or at least give you some direction to resolve your issue. 希望这有助于或至少为您提供一些方向来解决您的问题。

If you can use FrameLayout as parent to the ListView , then set the cornered shape as foreground to the parent FrameLayout . 如果可以使用FrameLayout作为ListView父级,则将角形形状设置为父级FrameLayout foreground

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="@drawable/list_border">

    <ListView
        android:id="@+id/listView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_selector"/>
    </FrameLayout>

If you want to use LinearLayout as parent of ListView, then surround your ListView with ForegroundLinearLayout and set the cornered shape as foreground to the ForegroundLinearLayout . 如果你想使用的LinearLayout作为ListView控件的父母,然后环绕你ListViewForegroundLinearLayout并设置有角的形状作为foregroundForegroundLinearLayout

What is ForegroundLinearLayout ? 什么是ForegroundLinearLayout It is LinearLayout with Foreground property like FrameLayout . 它是带有Foreground属性的LinearLayout ,如FrameLayout Get the code from Chris Banes. 从Chris Banes那里获取代码。 https://gist.github.com/chrisbanes/9091754 https://gist.github.com/chrisbanes/9091754

Your code should be: 你的代码应该是:

<?xml version="1.0" encoding="utf-8"?>
<ForegroundLinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/list_border">

<ListView
    android:id="@+id/listView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/list_selector"/>
</ForegroundLinearLayout>

please add padding in your style.i updated style here. 请在你的style.i更新样式添加填充。
2 list_border.xml 2 list_border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<padding android:left="10dp"
      android:right="10dp"
      android:top="10dp"
      android:bottom="10dp"
      />
</shape>

This can be done easily by using Android's support library's CardView . 这可以通过使用Android的支持库的CardView轻松完成。 Just wrap your ListView in a CardView layout and set a cardCornerRadius to the CardView , like this: 只需将ListView包装在CardView布局中,并将cardCornerRadius设置为CardView ,如下所示:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#cacaca">

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                card_view:cardCornerRadius="10dp"
                >

                <ListView
                    android:id="@+id/listView3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:listSelector="@drawable/list_selector"/>
            </android.support.v7.widget.CardView>
        </LinearLayout>

Dont forgot to import the CardView 's support library: CardView了导入CardView的支持库:

compile 'com.android.support:cardview-v7:23.1.1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM