java - How to specify width of custom view? -
i'm having trouble trying set specific size width of custom view. know can hard-code value in layout_width
field in xml
file i'm trying not result hard-coding values. need view
half size of screen. so, within class
find half screen size:
windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); display display = wm.getdefaultdisplay(); try{ point size = new point(); display.getsize(size); screenwidth = size.x; }catch(nosuchmethoderror e){ screenwidth = display.getwidth(); }
then, call:
measure(width|measurespec.exactly, layoutparams.wrap_content|measurespec.exactly); //where width equal half screen width
but not seem working case. so, sum up, how specify particular size custom view
? useful , appreciated! thanks!
override onmeasure()
method in custom view:
@override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int width = getresources().getdisplaymetrics().widthpixels >> 1; widthmeasurespec = measurespec.makemeasurespec(width, measurespec.exactly); super.onmeasure(widthmeasurespec, heightmeasurespec); }
basically, override width measurespec
half width of screen, pass through superclass.
Comments
Post a Comment