繁体   English   中英

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

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

我有一个顶部和底部有圆角的列表视图。 看起来像这个图像: 在此输入图像描述

但是当我单击ListView的顶部和底部时,List Item的背景是矩形而不是圆形作为ListView的顶部和底部背景。 喜欢这个图片:

列表项的角不圆

如何解决这个问题?

这是我的代码:

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

<?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

<?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

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);

这个解决方案并不漂亮,但它可以满足您的需求。 想法是反转角落并将它们绘制为前景:

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>

将前景( drawable/inverted_corners.xml )设置为圆孔 ,使用背景颜色绘制。 诀窍是在形状外画一条线 ,而不是填充形状:

<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>

这可以确保圆角位于选择器顶部,并具有过卷效果。

我建议你应该为你的ListView的第一个和最后一个元素设置带角的布局,而不是整个ListView

除了list_selector.xml之外,您还可以创建两个额外的.xml文件:一个用于第一个元素,顶部有圆角(让我们称之为list_selector_top.xml ),另一个用于最后一个元素,底部有圆角( list_selector_bottom.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 )。

它可能不是最简单的方法,但它有效。

将您的角半径设置为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>

所以你的选择不会是list view交叉边界。

在此输入图像描述

缺点 -你无法确定哪些radius你必须设置为每顶部和底部,这就是为什么要设置radius到四面八方。

希望这有助于或至少为您提供一些方向来解决您的问题。

如果可以使用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>

如果你想使用的LinearLayout作为ListView控件的父母,然后环绕你ListViewForegroundLinearLayout并设置有角的形状作为foregroundForegroundLinearLayout

什么是ForegroundLinearLayout 它是带有Foreground属性的LinearLayout ,如FrameLayout 从Chris Banes那里获取代码。 https://gist.github.com/chrisbanes/9091754

你的代码应该是:

<?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>

请在你的style.i更新样式添加填充。
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>

这可以通过使用Android的支持库的CardView轻松完成。 只需将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>

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