Android uses Java code to set layout, view width/height, or adaptive

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.