In the achat project, the length and width of the conversation content is self-adapting, but if the text content is too much, then the width is almost full, if you say and the other said that a lot of content, then all the screen is text, it is not easy to distinguish between what others say or their own said. Then you need to have an adaptive layout and then a width limit.
First, the layout_width/layout_height that defines layout in XML is set to Wrap_content, and then in the GetView method, the layout is initially wrap_content ( Why is there a wrap_content in the XML and set it again? Because of the reuse of layout, not much to say), Widgetcontroller.setlayoutwidth (Holder.lay_content, marginlayoutparams.wrap_content);
Then, after filling out the layout, another paragraph, so that the layout can not exceed the width of the screen 50%, if more than the maximum width:
int w= (int) (Densityutil.getscreenwidth () *0.5), if (Widgetcontroller.getwidth (holder.lay_content) >w) { Widgetcontroller.setlayoutwidth (holder.lay_content,w);}
Here is the code for the Setlayoutwidth method, which is quite handy:
/** * Set the width (pixels) of the view, if set to Marginlayoutparams.wrap_content * @param view * @param width */public static void Setlayoutwidth (View view,int width) {/* Marginlayoutparams margin=new marginlayoutparams (view.g Etlayoutparams ()); Margin.setmargins (Margin.leftmargin,y, Margin.rightmargin, y+margin.height); Relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (margin); View.setlayoutparams (Layoutparams); Viewgroup.marginlayoutparams layoutparams =newlayparms (view, margin); Relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (margin); View.setlayoutparams (Layoutparams); View.requestlayout (); */if (view.getparent () instanceof framelayout) {framelayout.layoutparams lp= (FrameLayout.Layou Tparams) View.getlayoutparams (); Lp.width=width; VIEW.SETLAYOUTPARAMS (LP); View.setx (x); View.requestlayout (); } else if (view.getpareNT () instanceof relativelayout) {relativelayout.layoutparams lp= (relativelayout.layoutparams) View.getLayoutParams ( ); Lp.width=width; VIEW.SETLAYOUTPARAMS (LP); View.setx (x); View.requestlayout (); } else if (View.getparent () instanceof linearlayout) {linearlayout.layoutparams lp= (linearlayout.layoutparams) view.g Etlayoutparams (); Lp.width=width; VIEW.SETLAYOUTPARAMS (LP); View.setx (x); View.requestlayout (); } }
Reprint Please specify Source: Http://blog.csdn.net/rocklee
Android uses Java code to set layout, view width/height, or adaptive