Android rounded corners in ListView

I have a ListView with rounded corners made using the following shape as a background:

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

The problem is the selector. It has a rectangle, so when you select the first or last element, the corners are no longer rounded. I found a very good solution in the last post at http://www.anddev.org/view-layout-resource-problems-f27/rounded-corners-on-listview-t8193-15.html . The problem is that I cannot get another class to inherit from ListView. How can I apply this method when the only thing I have is a link to an existing ListView? The reason I have to do this is because the layout is swollen from xml.

I am looking for something like:

 ListView lv = (ListView)findViewById(...); lv.onSizeChanged = protected void onSizeChanged(int w, int h, int oldw, int oldh){ ... } 

thank

+3
android listview selector rounded-corners
Jul 20 2018-11-12T00:
source share
3 answers

There seems to be no other way than extending the ListView class and using it in XML. Here is a sample code:

 public class WListView extends LinearLayout { // ================================================================= // Variables // ================================================================= private Path clipArea; // ================================================================= // Public methods // ================================================================= public WListView(Context context) { super(context); } public WListView(Context context, AttributeSet attr) { super(context, attr); } // ================================================================= // Private methods // ================================================================= @Override protected void onSizeChanged(int w, int h, int oldW, int oldH) { super.onSizeChanged(w, h, oldW, oldH); clipArea = new Path(); RectF rect = new RectF(0, 0, w, h); int cornerRadius = 13; // we should convert px to dp here clipArea.addRoundRect(rect, cornerRadius, cornerRadius, Path.Direction.CW); } @Override protected void dispatchDraw(Canvas canvas) { canvas.save(); canvas.clipPath(clipArea); super.dispatchDraw(canvas); canvas.restore(); } } 
+4
Sep 06 2018-11-11T00:
source share

You can implement it using 9-tone images used in the background. Please check the document for it on the Android website.

thank

0
Jul 20 '11 at 12:48
source share

You can make a transparent 9Patch image with rounded corners and use it as a mask for blending LinearLayout. So it doesn't matter if the selector expires - the mask will always override this problem.

I needed to find a good way to mask any of my layouts to create typical iOS-style layouts.

I wrote a fairly complete answer to the question: rounded cropped corners of Android XML

0
Nov 29 '11 at 15:15
source share



All Articles