java - Custom view not being inflated within FrameLayout -
after implementing framelayout within relativelayout, tried show custom view, it's not working reason. getactivity()
in sketcha d1 = new sketcha(getactivity());
becomes underlined in red , returns following error:
sketcha (context, attributeset) in sketcha cannot applied (fragmentactivity)
public class fragmentdrawings extends android.support.v4.app.fragment { public fragmentdrawings() {} @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_drawings, container, false); } @override public void onactivitycreated(@nullable bundle savedinstancestate) { view v = getview(); assert v != null; framelayout framelayout = getview().findviewbyid(r.id.sketcha); layoutparams params = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); sketcha d1 = new sketcha(getactivity()); d1.setlayoutparams(params); framelayout.addview(d1); super.onactivitycreated(savedinstancestate); } }
skectha.java (custom view)
public class skectha extends view { private final paint mblackpaint = new paint(); private final paint mredpaint = new paint(); private final textpaint mtextpaint; public static final int boxes_count = 3; private float onedp; private float windowheight; public skectha(context context, attributeset attrs) { super(context, attrs); onedp = typedvalue.applydimension(typedvalue.complex_unit_dip, 1, getresources().getdisplaymetrics()); windowheight = typedvalue.applydimension(typedvalue.complex_unit_dip, 10, getresources().getdisplaymetrics()); float textsize = typedvalue.applydimension(typedvalue.complex_unit_sp, 15, getresources().getdisplaymetrics()); mredpaint.setcolor(color.parsecolor("#cc3333")); mblackpaint.setcolor(color.black); mblackpaint.setstrokewidth(onedp); mblackpaint.setstyle(paint.style.stroke); mtextpaint = new textpaint(textpaint.anti_alias_flag); mtextpaint.setcolor(color.black); mtextpaint.settextalign(paint.align.center); mtextpaint.settextsize(textsize); mwindowpaint = new paint(); mwindowpaint.setcolor(color.parsecolor("#cc3333")); mwindowpaint.setstyle(paint.style.stroke); mwindowpaint.setstrokewidth(onedp); mwindowfilledpaint = new paint(); mwindowfilledpaint.setcolor(color.parsecolor("#cc3333")); mwindowfilledpaint.setstyle(paint.style.fill_and_stroke); mwindowfilledpaint.setstrokewidth(onedp); } private paint mwindowpaint; private paint mwindowfilledpaint; rectf rect = new rectf(); rectf rect2 = new rectf(); @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (getwidth() == 0) return; float msiderectwidth = typedvalue.applydimension(typedvalue.complex_unit_dip, 5, getresources().getdisplaymetrics()); setbackgroundcolor(color.parsecolor("#808080")); } private void fillrect(canvas canvas, float left, float right, string number) { rect.set(left + onedp / 2, 0 + onedp / 2, right - onedp / 2, getheight() - onedp / 2); float xpos = left + ((right - left) / 2); float ypos = (canvas.getheight() / 2) - ((mtextpaint.descent() + mtextpaint.ascent()) / 2); canvas.drawtext(number, xpos, ypos, mtextpaint); rect2.set(left + onedp / 2, 0 + onedp / 2, left + (right - left) * 20 / 100 - onedp / 2, windowheight - onedp / 2); } }
wiki
Comments
Post a Comment