你是否了解如何使用在今年 Google I/O 大会上宣布的 Constraint Layout 布局?
你是否了解如何使用在今年 Google I/O 大会上宣布的 Constraint Layout 布局?
你可以打开现有布局资源文件的可视化编辑器,并在RelativeLayout(例如)上右键单击,然后点击选项将其转换为ConstraintLayout。
此外,您还需要在build.gradle文件中添加Gradle依赖项:
compile 'com.android.support.constraint:constraint-layout:1.0.0'
来自文档
如果您正在更新现有项目,请按照以下步骤进行:
确保您拥有最新的Android Support存储库(版本32或更高版本):// 这是我缺少的部分
点击工具 > Android > SDK管理器。 单击SDK工具选项卡。 选择Android Support存储库,然后单击“确定”。
将更新后的约束布局库添加为依赖项到您的build.gradle文件中:
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.0'
}
在工具栏或同步通知中,点击“Sync Project with Gradle Files”来同步项目与Gradle文件。理解ConstraintLayout的性能优势描述了传统布局层次结构的开销。它举了一个使用嵌套布局构建此布局的例子。
这篇文章声称:
相比RelativeLayout,ConstraintLayout在测量/布局阶段的性能提高了约40%
这个Codelab项目展示了如何使用ConstraintLayout来减少View层次结构并扁平化上述布局。
repositories {
maven {
url 'https://maven.google.com'
}
}
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support.constraint:constraint-layout-solver:1.0.2'
修改 build.gradle
文件中的依赖项。
使用 compile com.android.support.constraint:constraint-layout:1.0.0-beta1
代替。
1) 使用ConstraintLayout设计新的布局,需要在app.gradle文件中包含依赖项。
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
....>
<View
android:id="@+id/top_view"
.../>
<View
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/top_view"
app:layout_constraintBottom_toTopOf="@+id/bottom_view"
..../>
<View
android:id="@+id/bottom_view"
.../>
</android.support.constraint.ConstraintLayout>
2) 将现有的布局文件转换为约束布局:
在Android Studio中打开您现有的布局,并选择编辑器窗口底部的设计选项卡。在组件树窗口中,右键单击根布局,然后单击“转换为ConstraintLayout”。接下来,包括上述定义的属性。
Google发布了官方版本1.0的ConstraintLayout
现在可以导入非beta版本了
compile 'com.android.support.constraint:constraint-layout:1.0.0'
查看这里的信息 http://tools.android.com/recent/constraintlayout10isnowavailable
com.android.support.constraint:constraint-layout:1.0.2
。 - MANISH PATHAK