博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Kotlin的Android约束布局
阅读量:2532 次
发布时间:2019-05-11

本文共 7418 字,大约阅读时间需要 24 分钟。

In this tutorial, we will discuss the Android ConstraintLayout attributes. We will learn how to position views based on constraints through XML layout and programmatically using Kotlin.

在本教程中,我们将讨论Android ConstraintLayout属性。 我们将学习如何通过XML布局以及使用Kotlin以编程方式基于约束来定位视图。

什么是Android ConstraintLayout? (What is Android ConstraintLayout?)

Android Constraint is RelativeLayout with additional features. They were introduced to prevent too many nested layouts. They flatten out the layouts.

Android约束是带有其他功能的RelativeLayout。 引入它们是为了防止过多的嵌套布局。 他们弄平了布局。

Some of the constraint layout featrues are:

约束布局的一些特征是:

  • Positioning sides of a view relative to sides of another view.

    相对于另一个视图的侧面放置一个视图的侧面。
  • ConstraintSets

    约束集
  • Setting horizontal/vertical bias

    设置水平/垂直偏置
  • Chaining and grouping views together

    将视图链接和分组在一起
  • Barriers

    壁垒
  • Setting the percent of width or height the view would occupy on the screen.

    设置视图将在屏幕上占据的宽度或高度的百分比。
  • Circular positioning the views.

    循环定位视图。

约束布局依赖性 (Constraint Layout Dependencies)

Please add the following dependency in your build.gradle file for constraint layout support.

请在build.gradle文件中添加以下依赖项,以获取约束布局支持。

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

Let’s look into some examples of using constraint layout in android apps.

让我们看一些在Android应用程序中使用约束布局的示例。

相对于彼此放置视图 (Positioning Views Relative to one another)

Let’s drag and drop a view into the center of the screen.

让我们将视图拖放到屏幕中心。

As you see in the above GIF, the Button is positioned in the center of the screen with each side having the constraint to the parent view.

如您在上面的GIF中所看到的,“按钮”位于屏幕的中央,每一侧都有对父视图的约束。

We can remove either a single constraint or all as shown below.

我们可以删除一个约束或全部约束,如下所示。

The cross button is used to remove ALL constraints. Clicking the circle would remove the constraint of that specific side.

十字按钮用于删除所有约束。 单击圆将删除该特定面的约束。

You can drag the same circle to create a new constraint.

您可以拖动相同的圆以创建新约束。

Let’s look at the XML layout for the above design.

让我们看一下上述设计的XML布局。

The parent refers to the root view.

父级是指根视图。

The list of XML attributes to position views by each other’s sides(top/left/bottom/right) is as follows.

用来按彼此的边(顶部/左侧/底部/右侧)放置视图的XML属性列表如下。

We can align two views by their baselines as well.

我们也可以通过它们的基线来对齐两个视图。

Let’s learn how to create constraints programmatically using Kotlin.

让我们学习如何使用Kotlin以编程方式创建约束。

约束集 (ConstraintSet)

The ConstraintSet instance is defined to set the constraints programmatically.

ConstraintSet实例被定义为以编程方式设置约束。

The activity_main.xml does not contain anything inside the ConstraintLayout tag. Let’s look at the main activity Kotlin code.

activity_main.xml在ConstraintLayout标记内不包含任何内容。 让我们看一下主要的活动Kotlin代码。

package net.androidly.androidlyconstraintlayoutimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.support.constraint.ConstraintLayoutimport android.support.constraint.ConstraintSetimport android.widget.Buttonimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {    val ID_1 = 1    val ID_2 = 2    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        val button = Button(this)        button.text = "Button"        button.id = ID_1        val lp = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)        button.layoutParams = lp        constraintLayout.addView(button)        val button2 = Button(this)        button2.text = "Button2"        button2.id = ID_2        constraintLayout.addView(button2)        val constraintSet = ConstraintSet()        //Copy all the previous constraints present in the constraint layout.        constraintSet.clone(constraintLayout)        constraintSet.connect(ID_1, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)        constraintSet.connect(ID_1, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)        constraintSet.connect(ID_1, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT)        constraintSet.connect(ID_1, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT)        constraintSet.connect(ID_2, ConstraintSet.BOTTOM, ID_1, ConstraintSet.TOP)        constraintSet.connect(ID_2, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT)        constraintSet.connect(ID_2, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT)        constraintSet.applyTo(constraintLayout)    }}

The clone() function is used to copy all the previous constraints defined in the constraint layout.

clone()函数用于复制约束布局中定义的所有先前约束。

In the above code, we are setting the first button in the center of the layout and the second button is on top of it.

在上面的代码中,我们将第一个按钮设置在布局的中心,第二个按钮在其顶部。

The connect() function has an additional fifth parameter as well for margin values.

connect()函数还有一个附加的第五个参数,用于边距值。

parent in XML layout is
父级等效项为
ConstraintSet.PARENT_ID.
ConstraintSet.PARENT_ID

水平和垂直偏差 (Horizontal and Vertical Bias)

The attributes for setting bias are: app:layout_constraintVertical_bias and app:layout_constraintHorizontal_bias.

设置偏差的属性为: app:layout_constraintVertical_biasapp:layout_constraintHorizontal_bias

They expect a value between 0 and 1, and 0.5 is the default value.

他们期望一个介于0和1之间的值,默认值为0.5。

约束的链接和分组 (Chaining and Grouping of Constraints)

Chains are used to evenly space views horizontally or vertically.

链用于在水平或垂直方向上均匀分布视图。

The xml layout code:

xml布局代码:

The chain styles can be of the following types.

链样式可以是以下类型。

  • spread

    传播
  • spread inside

    散布在里面
  • packed

    包装好的
  • weighted

    加权的

The weighted style requires setting weight on each of the views. The view with most weight occupies the most space.

加权样式需要在每个视图上设置权重。 重量最大的视图占用最多的空间。

The app:layout_constraintHorizontal_weight is used to set the weights in a horizontal chain.

app:layout_constraintHorizontal_weight用于设置水平链中的权重。

Groups are used to toggle the visibility of a group of views together.

用于将一组视图的可见性一起切换。

Add the following inside XML code to the previously defined constraint layout.

将以下内部XML代码添加到先前定义的约束布局中。

壁垒 (Barriers)

A Barrier is used to create a virtual divider or a guideline on a group of views based on the largest view in the group.

障碍用于基于组中的最大视图在一组视图上创建虚拟分隔线或准则。

This virtual divider can be connected with other child views.

该虚拟分隔线可以与其他子视图连接。

ConstraintLayout百分比宽度和高度 (ConstraintLayout Percentage Width and Height)

Constraint Layouts support percentage width and height, just like .

约束布局支持宽度和高度的百分比,就像一样。

For percentage width, you have to set the Button’s width to 0dp and vice-versa for percentage height.

对于百分比宽度,必须将Button的宽度设置为0dp,反之亦然。

约束布局的圆形定位 (Circular Positioning of Constraint Layout)

We can position the views at radial distances and angles from one another. The app:layout_constraintCircle is used to reference the view, which would act as the center of the circle.

我们可以将视图放置在彼此之间的径向距离和角度处。 app:layout_constraintCircle用于引用视图,该视图将作为圆的中心。

The app:layout_constraintCircleRadius and app:layout_constraintCircleAngle are used to set the radius distance from the center of the circle and the angle at which our view would be positioned.

app:layout_constraintCircleRadiusapp:layout_constraintCircleAngle用于设置距圆心的半径距离和视图定位的角度。

翻译自:

转载地址:http://suqzd.baihongyu.com/

你可能感兴趣的文章
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>