官术网_书友最值得收藏!

The constraint layout

The constraint layout is similar to the relative layout, in that it allows us to generate complex layouts without having to create memory sapping, view group hierarchies. Android Studio makes creating such layouts far easier, because it provides a visual editor that enables us to drag and drop not only screen components, but also their connections. Being able to experiment with layout structures so easily provides us with a great sandbox environment to  develop new layouts.

The following exercise will take you through the process of installing the constraint library so that you can begin experimenting yourself.

  1. As of Android Studio 3.0 the ConstraintLayout is downloaded by default, but if you want to update an earlier project, you will need to open the SDK manager. The ConstraintLayout and constraint solver can both be found under the SDK Tools tab, as follows:
The constraint layout API
  1. Check the Show Package Details box and make a note of the version number as this will be required shortly.
  2. Next, add the ConstraintLayout library to our dependencies. The simplest way to do this is selecting your module and then the Dependencies tab of the Project Structure dialog, which can be accessed from the File menu.
  3. The constraint library can be found by clicking on the + button and then 1 Library dependency and selecting it from the list.
  4. Finally, synchronize your project from the toolbar, the build menu, or Ctrl + Alt + Y.

This is the simplest way to add module dependencies, but it is always good as developers to understand what is going on under the hood. In this case, we could have added the library manually by opening the module level build.gradle file and adding the following, highlighted text to the dependencies node:

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-
core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'junit:junit:4.12'

Those of you that have developed with the relative layout will be familiar with commands such as layout_toRightOf or layout_toTopOf. These attributes can still be applied to a ConstraintLayout, but there are more. In particular, the ConstraintLayout allows us to position views based on individual sides, for example layout_constraintTop_toBottomOf?, which aligns the top of our view to the bottom of the one specified.

Useful documentation on these attributes can be found at: developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html.
主站蜘蛛池模板: 随州市| 阿尔山市| 长寿区| 渝北区| 民乐县| 集贤县| 棋牌| 杨浦区| 泰和县| 凌海市| 页游| 台山市| 颍上县| 马边| 洛川县| 南京市| 云阳县| 胶南市| 东乡族自治县| 陕西省| 上蔡县| 博白县| 定兴县| 沂南县| 罗定市| 台湾省| 景德镇市| 华池县| 桐庐县| 镇远县| 肃北| 彭泽县| 化州市| 博罗县| 九寨沟县| 甘孜县| 和林格尔县| 进贤县| 霍山县| 惠安县| 海宁市|