繁体   English   中英

Android中列表项的背景图像隐藏的ListView圆角

[英]ListView rounded corner hidden by list item's background image in android

在此处输入图片说明使用填充...屏幕调用时 我给listview加了边框。 并在列表项中提供背景图片。 因此,由于列表项的背景图片listview的边框被隐藏。

列表显示

<ListView
        android:id="@+id/listViewsea"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginBottom="5dp"
        android:cacheColorHint="#0000"
        android:clipToPadding="true"
        android:scrollbars="none"
        android:listSelector="@android:color/transparent"
        android:divider="#00000000"
        android:paddingBottom="5dp"
        android:background="@drawable/cornerborder"
        >
    </ListView>

cornerborder.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="1dp"
        android:color="#457DB6" />

    <gradient
        android:angle="225"
        android:endColor="#457DB6"
        android:startColor="#457DB6" />

    <corners
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp" />

</shape>

在listAdapter中

if(position%2 == 0)
            {
                    convertView.setBackgroundResource(R.drawable.commonrow_type_1_straight);

            }else 
            {
                                convertView.setBackgroundResource(R.drawable.commonrow_type_2_straight);

            }

感谢上帝终于有了解决方案........这里是XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@drawable/lineargallarybackground"
        android:clipChildren="true"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textlisttitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/titlebackground"
            android:gravity="center_horizontal|center_vertical"
            android:text="Kuwait" >
        </TextView>
        <ListView
            android:id="@+id/listViewsea"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginBottom="5dp"
            android:cacheColorHint="#0000"
            android:clipChildren="true"
            android:clipToPadding="true"
            android:divider="#00000000"
            android:listSelector="@android:color/transparent"
            android:scrollbars="none" >
        </ListView>
     </LinearLayout>

我已经在Linearlayout中设置了背景图片。

如果有人(像我一样)正在寻找一种将listview推到四舍五入边缘而不添加填充的解决方案,这应该可以解决问题:

尝试对ListView(或包含布局)进行子类化,并按如下方法重写OnDraw方法,用RADIUS_IN_PIXELS的值代替:

@Override
protected void onDraw(Canvas canvas) {
    Path clipPath = new Path();
    clipPath.addRoundRect(new RectF(canvas.getClipBounds()), RADIUS_IN_PIXELS, RADIUS_IN_PIXELS, Path.Direction.CW);
    canvas.clipPath(clipPath);
    super.onDraw(canvas);
}

...并创建一个类似“舍入”的自定义可绘制对象,替换为YOUR_BACKGROUND_COLOR和RADIUS_IN_DP,确保将DP中矩形的舍入与PX中先前的剪切半径匹配:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="RADIUS_IN_DP" />
    <solid android:color="YOUR_BACKGROUND_COLOR"/>
</shape>

然后,您可以在布局中使用该子类,将以下行添加到ListView或包含布局:

android:background="@drawable/rounded"
android:clipChildren="true"

所有子项都将剪辑到您在OnDraw()覆盖中指定的边界,并且将基于您的“四舍五入”可绘制对象添加背景。

暂无
暂无

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

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