Automatic line wrapping for Android controls

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/u011176685/article/details/47373503

First, the problem description

A. Getting data from the background in the Get Rectangle box in the project, and now there are 7 or 8 words in some regions. When a resolution of 480*800 is encountered, the number of views is squeezed out. Does not fit all resolutions and affects the user experience.

B. There's a lot of information on the Web, with Android LinearLayout automatically customizing a view and adding the view to it. But because my project uses a lot of auto-wrapping, and feel the code can be less or less points. If you use more friends, you can refer to the next.


Second, solve the problem

A. Rectangle-box XML layout file

<relativelayout android:layout_width= "Fill_parent" android:layout_height=                        "Wrap_content" android:id= "@+id/job_details_fixgl" > <!--Recruiting Area--                            <textview android:id= "@+id/job_details_aty_area_tv"                            Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"                            android:drawableleft= "@drawable/icon_classify_coord" android:drawablepadding= "3DP"                        Android:textcolor= "#999999" tools:text= "Yubei District Yubei Yubei"/> <!--published <textview android:layout_torightof= "@+id/ Job_details_aty_area_tv "android:id=" @+id/job_details_aty_date_tv "a Ndroid:layout_width= "WraP_content "android:layout_height=" Wrap_content "Android:drawableleft = "@drawable/icon_time" android:drawablepadding= "3DP" Android:layout_  marginleft= "5DP" android:textcolor= "#999999" tools:text= "2015.04.12" /> <!--views-<textview android:id=                            "@+id/job_details_aty_views_tv" android:layout_width= "Wrap_content"                            android:layout_height= "Wrap_content" android:layout_marginleft= "5DP"                            android:drawableleft= "@drawable/icon_eye" android:drawablepadding= "3DP" Android:textcolor= "#999999" tools:text= "1233 Times"/> </relativelayou T>

B.activity inside the code snippet

                    String date = "2015.4.12";                    string address = "Jiangbei District" string count= "123" + "secondary"; Calculates the respective widths of 3 controls, float addwidth= tvarea.getpaint (). Measuretext (address) + densityutil.dip2px (jobdetailsact Ivity.this, 15);//Location float datewidth= datetv.getpaint (). Measuretext (date) + densityutil.dip2px (jobdet ailsactivity.this,15);//Date float countwidth=viewstv.getpaint (). Measuretext (count) + densityutil.dip2px (J                    obdetailsactivity.this,10);//number of views int detailswidth= lldetails.getwidth ();                    Relativelayout.layoutparams lp= (Relativelayout.layoutparams) viewstv.getlayoutparams ();                        if (addwidth+datewidth+countwidth>=detailswidth) {///If the width of the 3 control is greater than the width of the parent container, modify the position of the 3rd control dynamically, and adjust to the bottom of the 1th control                        Lp.addrule (RELATIVELAYOUT.BELOW,R.ID.JOB_DETAILS_ATY_AREA_TV);          Lp.setmargins (0,0,0,0);              VIEWSTV.SETLAYOUTPARAMS (LP); }else{//If the width of the 3 control is less than the width of the parent container, the position of the 3rd control is dynamically modified to the 2nd right lp.addrule (relativelayout.                        RIGHT_OF,R.ID.JOB_DETAILS_ATY_DATE_TV);                    VIEWSTV.SETLAYOUTPARAMS (LP);                    } tvarea.settext (address);                    Datetv.settext (date); Viewstv.settext (count);

simply explain that 15,10 is not set casually. TextView the picture on the left is 14*20px, so the width is 7DP (1dp approximately equal to 2px) between the picture and the text spacing 3DP distance from the right control's margin 5DP 7+3+5=15DP. Densityutil is a tool class. About dip turn px Here is not the story.

C. If something is unclear, it can be presented. Please also note that there is a wrong place to write. Learn from each other. The program Ape is a continuous learning process.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Automatic line wrapping for Android controls

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.