问题描述
我有listview,圆角像波纹管一样实现.
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/list_bg" android:divider="#dbdbdb" android:dividerHeight="1sp" android:cacheColorHint="#00000000" android:descendantFocusability="afterDescendants" android:scrollbars="vertical"> </ListView>
其中list_bg.xml是:
<?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:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> </shape>
这给了我圆角. 问题是,每个项目都以右侧的左侧和图像布顿的TextView进行相关.两个视图都有自定义图形,在按下,漏气和默认状态.类似的东西.
+------------------------------------+ | TextView | ImageButton | +------------------------------------+
问题是自定义图形封面ListView背景,并因此在第一项和最后一个项目上舍入的角落.滚动时,一切都好.
我已经写了自定义适配器,在getView方法中,我为项目设置了正确的BG.
@Override public View getView(int position, View view, ViewGroup parent) { ... if(position == 0) { mItemView.setBackgroundResource(R.drawable.cell_item_first_selector); mImgBtn.setBackgroundResource(R.drawable.cell_btn_first_selector); } else if (position == mData.size() -1) { mItemView.setBackgroundResource(R.drawable.cell_item_last_selector); mImgBtn.setBackgroundResource(R.drawable.cell_btn_last_selector); } else { mItemView.setBackgroundResource(R.drawable.cell_item_def_selector); mImgBtn.setBackgroundResource(R.drawable.cell_btn_def_selector); } ... }
它的工作,但我不确定这是好的解决方案.是否有任何其他方法可以在Android视图中自动舍入,而不仅仅是通过设置背景?
推荐答案
public class ClippedListView extends ListView { /** * @param context */ public ClippedListView(Context context) { super(context); } /** * @param context * @param attrs */ public ClippedListView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void dispatchDraw(Canvas canvas) { float radius = 10.0f; Path clipPath = new Path(); RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()); clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); canvas.clipPath(clipPath); super.dispatchDraw(canvas); } }
首先剪切不起作用,然后使用
setlayertype(View.Layer_type_software,null)
在我的Clipplistview上解决问题.我的物品的背景不再搞乱了我的角落!
问题描述
I have ListView with rounded corners implemented like bellow.
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/list_bg" android:divider="#dbdbdb" android:dividerHeight="1sp" android:cacheColorHint="#00000000" android:descendantFocusability="afterDescendants" android:scrollbars="vertical"> </ListView>
where list_bg.xml is:
<?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:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> </shape>
This give me rounded corners. The problem is that each items are RelativeLayout with TextView on the left side and ImageButton on the right side. Both views have custom graphic on pressed, foucused and default state. Something like this.
+------------------------------------+ | TextView | ImageButton | +------------------------------------+
The problem is that custom graphic cover ListView background and thus rounded corners on first and last item. Everything is okay when scrolling.
I've written custom adapter and in getView method I set proper bg for items.
@Override public View getView(int position, View view, ViewGroup parent) { ... if(position == 0) { mItemView.setBackgroundResource(R.drawable.cell_item_first_selector); mImgBtn.setBackgroundResource(R.drawable.cell_btn_first_selector); } else if (position == mData.size() -1) { mItemView.setBackgroundResource(R.drawable.cell_item_last_selector); mImgBtn.setBackgroundResource(R.drawable.cell_btn_last_selector); } else { mItemView.setBackgroundResource(R.drawable.cell_item_def_selector); mImgBtn.setBackgroundResource(R.drawable.cell_btn_def_selector); } ... }
It works, but I'm not sure that is good solution. Is there any other way to automatically round corners in Android Views, not just by setting background?
推荐答案
public class ClippedListView extends ListView { /** * @param context */ public ClippedListView(Context context) { super(context); } /** * @param context * @param attrs */ public ClippedListView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void dispatchDraw(Canvas canvas) { float radius = 10.0f; Path clipPath = new Path(); RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()); clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); canvas.clipPath(clipPath); super.dispatchDraw(canvas); } }
First clipping was not working, then using
setLayerType(View.LAYER_TYPE_SOFTWARE, null)
on my clippedListView solve the issue. My items' background are not messing with my corners anymore!