Android-ConstraintLayout(约束布局)-V

作者: MonkeyLei | 来源:发表于2019-07-10 10:31 被阅读4次

Virtual Helper objects翻译叫什么虚拟助手对象。什么鬼哟。

In addition to the intrinsic capabilities detailed previously, you can also use special helper objects inConstraintLayoutto help you with your layout. Currently, theGuidelineobject allows you to create Horizontal and Vertical guidelines which are positioned relative to theConstraintLayoutcontainer. Widgets can then be positioned by constraining them to such guidelines. In1.1,BarrierandGroupwere added too.

解释:就是说是一个很特别的约束布局,帮助你更好的布局。目前有三种方式。早期有Guideline,1.1里面增加了Barrier和Group。

Barrier前面有说过https://zhuanlan.zhihu.com/p/37988125

官方网址也来个https://developer.android.google.cn/reference/android/support/constraint/Barrier?hl=zh-cn

Guideline

Guideline

public class Guideline
extends [View](https://link.zhihu.com/?target=http%3A//developer.android.google.cn/reference/android/view/View.html%3Fhl%3Dzh-cn)

java.lang.Objectandroid.view.View ↳android.support.constraint.Guideline

Utility class representing a Guideline helper object for [ConstraintLayout](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.html%3Fhl%3Dzh-cn). Helper objects are not displayed on device (they are marked as View.GONE) and are only used for layout purposes. They only work within a [ConstraintLayout](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.html%3Fhl%3Dzh-cn).

A Guideline can be either horizontal or vertical:

  • Vertical Guidelines have a width of zero and the height of their [ConstraintLayout](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.html%3Fhl%3Dzh-cn)parent
  • Horizontal Guidelines have a height of zero and the width of their [ConstraintLayout](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/support/constraint/ConstraintLayout.html%3Fhl%3Dzh-cn)parent

Positioning a Guideline is possible in three different ways:

  • specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin)
  • specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)
  • specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)

Widgets can then be constrained to a Guideline, allowing multiple widgets to be positioned easily from one Guideline, or allowing reactive layout behavior by using percent positioning.

解释:其实就是设置一个辅助线,可以是水平的,也可以是垂直的。可以用layout_constraintGuide_begin、layout_constraintGuide_end、layout_constraintGuide_percent分别设置距离父容器的距离或者百分比。

     <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline"
            app:layout_constraintGuide_begin="100dp"
            android:orientation="vertical"/>

    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            app:layout_constraintLeft_toLeftOf="@+id/guideline"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

有什么用了?

如果有很多控件都需要一个距离左侧一定距离(或者以屏幕宽度的比例的距离),那么我们就可以做这样一个辅助线。然后其他控件设置app:layout_constraintLeft_toLeftOf="@+id/guideline"就可以了。当然不排除你可能会用其他布局去实现。就看哪种更方便了。

image

***Group ***Group用于控制多个控件的可见性,好理解塞...

   <android.support.constraint.Group
              android:id="@+id/group"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="visible"
              app:constraint_referenced_ids="button4,button9" />

以前如果要做多个控件整体隐藏,可能需要外层嵌套布局或者分别做隐藏,写很多行代码,对吧。而且时间长,看起来也费劲。利用Group就可以做分组,不用嵌套,更容易管理。这也许就是约束布局不想做嵌套以后,group为了满足隐藏显示需求而特意出现的吧!!

另外还有一些关于ConstraintSet、ConstraintLayout.LayoutParams的一些知识。ConstraintSet后面继续.clone的方式生成或者克隆布局约束出来,具体的后面看看怎么搞。像LayoutParams这些和以前用法差不多吧。前几张知识了解了,也大概知道怎么用了。

有些属性看api吧https://developer.android.google.cn/reference/android/support/constraint/package-summary?hl=zh-cn

到这里就短暂结束了先。断断续续学了一段时间,也实际项目使用了。有什么特别的再总结吧。后面开始其他知识了, shape(不难,大概一两篇解决),然后自定义View(其实大部分时候做项目都是自定义View),事件分发等等。。。。东西还在多。。。。

相关文章

网友评论

    本文标题:Android-ConstraintLayout(约束布局)-V

    本文链接:https://www.haomeiwen.com/subject/vreckctx.html