Percentage of Android Development Layout Library

Source: Internet
Author: User


In order to solve the Android phone adaptation problem, we often think that if you can be a percentage of the interface layout, so it is easier to adapt to a variety of screens! Now Google officially provides a percentage layout support library (android-support-percent-lib).

This library provides:

Two kinds of layout for everyone to use: Percentrelativelayout, Percentframelayout, by name can be seen, this is inherited from Framelayout and relativelayout two container classes;

The supported properties are:

Layout_widthpercent, Layout_heightpercent,

Layout_marginpercent, Layout_marginleftpercent,

Layout_margintoppercent, Layout_marginrightpercent,

Layout_marginbottompercent, Layout_marginstartpercent, Layout_marginendpercent.

You can see support for the width and height, as well as margin.

In other words, as long as in the development process using Percentrelativelayout, Percentframelayout replacement framelayout, Relativelayout can.

Use:

On the use of, in fact, and simple, and GitHub also have examples, android-percent-support-lib-sample.

Use Android Studio to add the following information to the Build.gradle to get the support library, of course, if you do not download to the support library will prompt you to download.

{5330c3e9-d43a-40e0-a247-83c217be9a84}

Percentrelativelayout Use small Example:

The layout files are as follows:

 <android.support.percent.percentrelativelayout     xmlns:android= "http://
Schemas.android.com/apk/res/android "    xmlns:app=" Http://schemas.android.com/apk/res-auto "    android:layout_width=" Match_parent "    android:layout_height=" Match_parent ">       <textview         

Android:id= "@+id/row_one_item_one"         android:layout_width= "0DP"         android:layout_height= "0DP"          android:layout_alignparenttop= "true"         android: Background= "#7700ff00"         android:text= "w:70%,h:20%"          android:gravity= "Center"          AppLayout_heightpercent= "20%"         app:layout_widthpercent= "70%"/>       <textview         android:id= "@+id/row _one_item_two "        android:layout_width=" 0DP "         android:layout_height= "0DP"          android:layout_torightof= "@+id/row_one_item_one"         android: Background= "#396190"         android:text= "w:30%,h:20%"          app:layout_heightpercent= "20%"          android:gravity= "center"         app:layout_widthpercent= "30%"/ >         <imageview         android: Id= "@+id/row_two_item_one "        android:layout_width=" Match_parent "         android:layout_height= "0DP"          android:scaletype= "Centercrop"         android:layout

_below= "@+id/row_one_item_one"         android:background= "#d89695"         app:layout_heightpercent= "70%"/>        <textview         android:layout_width= "0DP"          android:layout_height= "0DP"          android:layout_below= "@id/row_two_item_one"         android:background= "#770000ff"         android:gravity= "center"          android:text= "width:100%,height:10%"         app:

Layout_heightpercent= "10%"         app:layout_widthpercent= "100%"/>   </android.support.percent.percentrelativelayout

The effect chart is as follows

Because there is no linearlayout we can come to our own definition. Just on the internet to see a custom good percentlinearlayout, share to everyone

public class percentlinearlayout extends linearlayout{    private

 PercentLayoutHelper mPercentLayoutHelper;       public percentlinearlayout (context context, attributeset 

Attrs)     {        super (context, attrs);           mPercentLayoutHelper = new 

Percentlayouthelper (this); &NBSP;&NBSP;&NBSP;&NBSP}          @Override     protected  Void onmeasure (int widthmeasurespec, int heightmeasurespec)     {         mpercentlayouthelper.adjustchildren (widthMeasureSpec, 

HEIGHTMEASURESPEC);

        super.onmeasure (WIDTHMEASURESPEC,&NBSP;HEIGHTMEASURESPEC);         if  (Mpercentlayouthelper.handlemeasuredstatetoosmall ())          {            super.onmeasure (

WIDTHMEASURESPEC,&NBSP;HEIGHTMEASURESPEC); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }       @ Override     protected void onlayout (boolean changed, int l,  INT&NBSP;T,&NBSP;INT&NBSP;R,&NBSP;INT&NBSP;B)     {      

  super.onlayout (CHANGED,&NBSP;L,&NBSP;T,&NBSP;R,&NBSP;B);

        mpercentlayouthelper.restoreoriginalparams (); &NBSP;&NBSP;&NBSP;&NBSP}        @Override     public  Layoutparams generatelayoutparams (attributeset attrs)     {         return new layoutparams (GetContext (),  attrs); &NBSP;&NBSP;&NBSP;&NBSP}         public static class layoutparams  extends linearlayout.layoutparams              implements percentlayouthelper.percentlayoutparams     {    

    private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;           public layoutparams (context c,  Attributeset attrs)         {      

      super (C, attrs);             mPercentLayoutInfo = 

Percentlayouthelper.getpercentlayoutinfo (C, attrs);         }            @Override          Public percentlayouthelper.percentlayoutinfo getpercentlayoutinfo ()          {            return 

Mpercentlayoutinfo; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           @ Override         protected void setbaseattributes (TypedArray

&NBSP;A,&NBSP;INT&NBSP;WIDTHATTR,&NBSP;INT&NBSP;HEIGHTATTR)         {             

Percentlayouthelper.fetchwidthandheight (THIS,&NBSP;A,&NBSP;WIDTHATTR,&NBSP;HEIGHTATTR); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           public  layoutparams (int width, int hEight)  {            super (width, height)

; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}              Public layoutparams (viewgroup.layoutparams source)  {       

     super (source); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}           public  layoutparams (marginlayoutparams source)  {        

    super (source);         }       }    }


The use effect is as follows:

Related Article

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.