padding not work expected in custom viewgroup
hi:
my layout have following structure:
relativelayout(with padding) mainview(viewgroup) and activety_main.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:paddingbottom="16dp" android:paddingleft="16dp" android:paddingright="16dp" android:paddingtop="16dp" tools:context=".mainactivity" > <com.example.testandroid.mainview android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout> mainview:
public class mainview extends viewgroup { public mainview(context context, attributeset as) { super(context); addview(new myview(context, null)); } @override protected void dispatchdraw(canvas canvas) { canvas.drawcolor(color.green); super.dispatchdraw(canvas); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { (int = 0, len = getchildcount(); < len; i++) { view v = getchildat(i); v.layout(l,t,r,b); } } } myview:
public class myview extends surfaceview implements callback { public myview(context contex, attributeset as) { super(contex, as); getholder().addcallback(this); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { refresh(); } private void refresh() { surfaceholder holder = getholder(); synchronized (holder) { canvas canvas = holder.lockcanvas(); if (canvas != null) { try { paint p = new paint(); p.setcolor(color.red); canvas.drawcircle(10, 10, 10, p); } { holder.unlockcanvasandpost(canvas); } } } } @override public void surfacecreated(surfaceholder holder) { refresh(); } @override public void surfacedestroyed(surfaceholder holder) { } } now, got now:

as can see, viewgroup created mainview has background of green. , mainview add myview dynamically background black here.
now, problem myview have padding relative mainview? because want take space of mainview, that's say, greed should invisible.
why?
when layout children, needs relative position.
to fill child.layout(0, 0, getwidth(), getheight());.
Comments
Post a Comment