introducedAndroid Studio
Introducing AutoLayout into the
dependencies {
Compile project (': AutoLayout ')
}
You can also directly
dependencies {
compile ' com.zhy:autolayout:1.4.3 '
}
Eclipse
It is recommended to use as for easy version updates. Really not, only copy and paste the source code. usage The first step:
In the androidmanifest of your project, indicate the size of your design manuscript.
<meta-data android:name= "Design_width" android:value= "768" >
</meta-data>
<meta-data Android:name= "Design_height" android:value= ">"
</meta-data>
Step Two:
Let your activity inherit from the autolayoutactivity.
In a very simple two-step process, you can start to happily write the layout and refer to sample for details. Other usage
If you do not want to inherit autolayoutactivity, you can write a layout file, LinearLayout, Autolinearlayout relativelayout, Autorelativelayout Framelayout-Autoframelayout
This can also be done with adaptation. currently supports properties layout_width layout_height layout_margin (left,top,right,bottom) pading (Left,top,right,bottom) TextSize maxWidth, MinWidth, MaxHeight, minheight configuration
The height that is used by default is the available height of the device, which does not include the status bar and the bottom of the action bar, if you want to take a percentage of the physical height of the device:
You can set it in the OnCreate method of application:
public class Usedevicesizeapplication extends application
{
@Override public
void OnCreate ()
{
Super.oncreate ();
Autolayoutconifg.getinstance (). Usedevicesize ();
}
}
Preview
We all know that when writing layout files, can not be real-time to preview the effect, then the experience is really very bad, but also to a large extent to reduce the development efficiency, so the following teach you how to use good, with the preview (for the library).
First of all, you need to remember the size of your design, such as 768 * 1280
Then in your preview panel, select the device with the same resolution as the design diagram:
Then you can see the most accurate previews:
Two notes: The size of the design you give to your UI is not a mainstream design, and you can create a new device on your own. Don't look at all the resolutions in preview, it's not going to fit, because some of the calculations are dynamic. Extended
For other inherited system Framelayout, LinearLayout, relativelayout control, such as CardView, if you want to further its internal directly support "px" percentage, you can extend the way, the extension is the following code, Also refer to issue#21:
Package Com.zhy.sample.view;
Import Android.content.Context;
Import Android.support.v7.widget.CardView;
Import Android.util.AttributeSet;
Import Com.zhy.autolayout.AutoFrameLayout;
Import Com.zhy.autolayout.utils.AutoLayoutHelper;
/** * Created by Zhy on 15/12/8.
*/public class Autocardview extends CardView {private final autolayouthelper mhelper = new Autolayouthelper (this);
Public Autocardview (Context context) {super (context);
} public Autocardview (context context, AttributeSet Attrs) {Super (context, attrs); } public Autocardview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, DefS
TYLEATTR); } @Override Public Autoframelayout.layoutparams generatelayoutparams (AttributeSet attrs) {return new
Autoframelayout.layoutparams (GetContext (), attrs); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {if (!isinEditMode ()) {Mhelper.adjustchildren ();
} super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
}
Precautions
The adaptation of the ListView, Recyclerview class item
Sample contains the ListView, recyclerview example, to see sample for the ListView
For item with ListView controls like this, the default root local write "px" is not valid because the outer layer is not autoxxxlayout, but a listview. But, don't be afraid, a line of code can support:
@Override public
View getView (int position, view Convertview, ViewGroup parent)
{
Viewholder holder = null;< C5/>if (Convertview = = null)
{
holder = new Viewholder ();
Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.list_item, parent, false);
Convertview.settag (holder);
For the ListView, note that adding this line allows you to use the height
autoutils.autosize (convertview) on item,
} else
{
Holder = ( Viewholder) Convertview.gettag ();
}
return convertview;
}
Note Autoutils.autosize (convertview); The location of this line of code. The demo also has relevant examples. For Recyclerview
Public Viewholder (View Itemview)
{
super (Itemview);
Autoutils.autosize (Itemview);
}
//...
@Override public
Viewholder Oncreateviewholder (viewgroup parent, int viewtype)
{
View Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.recyclerview_item, parent, false);
return new Viewholder (Convertview);
}
Be sure to remember Layoutinflater.from (Mcontext). Inflate method that uses three parameters. Specify the value of the setting reference width or height
Because of the characteristics of the library, the 1px on the width of the layout file is unequal, so if you need to maintain a consistent condition, the layout uses attributes:
App:layout_auto_basewidth= "Height", which represents the pixel value reference width written on height.
app:layout_auto_baseheight= "width", which represents the pixel value reference height written on width.
If you need to specify multiple values, the reference width is:
App:layout_auto_basewidth= "Height|padding"
Separated by |, like the use of gravity, the value is: Width,height margin,marginleft,margintop,marginright,marginbottom padding,paddingleft, Paddingtop,paddingright,paddingbottom textSize. the height problem of TextView
Design draft generally will only identify a font size, such as you set textsize= "20px", in fact, TextView occupy the height is certainly greater than 20px, the word will have a certain gap between the top and bottom, so must be flexible to write the height of the font, For example, the text up and down margin can be selected as small as possible. Or choose a different constraint to locate (for example, the MarginBottom is selected)