constraintlayout使用详解,减少嵌套ui,提升性能(代码片段)

gdutxiaoxu gdutxiaoxu     2023-01-30     526

关键词:

前言

对于初学者来说,可能觉得ConstraintLayout属性多,且属性长而弃用它,那你错失了这个大宝贝。

因为在复杂布局,我们会一直用RelativeLayout和LinearLayout去嵌套,因为嵌套的ViewGroup会导致手机多次测量和绘制,从而影响性能,如果嵌套严重可能出现掉帧或卡顿。

使用ConstraintLayout一招入魂。一句话概括是:传统布局能实现的,它能轻松实现实现。传统布局不能实现的,它也能实现。

一、为什么要用呢?

这里举个2个简单的例子。

1.1、例1

如图下图所示,我们分别用RelativeLayout和ConstraintLayout去实现它:

1.1.1、使用RelativeLayout实现如下

<RelativeLayout...>


    <TextView
        android:id="@+id/txt_a"
        android:layout_centerHorizontal="true"
        .../>

    <RelativeLayout
        android:layout_alignTop="@+id/txt_a"
        android:layout_toLeftOf="@+id/txt_a"
        android:layout_alignBottom="@+id/txt_a"
        ...>

        <TextView
            android:layout_centerInParent="true"
            android:id="@+id/txt_b"
            .../>

    </RelativeLayout>


</RelativeLayout>

这里使用了伪代码,把无关紧要的属性去掉了。相信懂的人都明白。这里用图层表示下,如下:

  • 最外层是 RelativeLayout(根布局)
  • 红色TextView_A 在顶部且横向居中。
  • 使用绿色 RelativeLayout上边和下边和A齐平,从而保证绿色RelativeLayout高度和A一样。且宽度满屏,在A左边
  • 蓝色TextView_B 在绿色RelativeLayout里居中。

那么接下来看看ConstraintLayout如何实现?

1.1.2、使用ConstraintLayout实现如下

<androidx.constraintlayout.widget.ConstraintLayout...>

    <TextView
        android:id="@+id/txt_a"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        .../>

    <TextView
        app:layout_constraintRight_toLeftOf="@+id/txt_a"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/txt_a"
        app:layout_constraintBottom_toBottomOf="@+id/txt_a"
        android:id="@+id/txt_b"
        .../>

</androidx.constraintlayout.widget.ConstraintLayout>
复制代码

我们继续看下,他的图层关系,真的简洁。

1.2、例2

如图下图所示,我们分别用RelativeLayout和ConstraintLayout去实现它:

在xml里。我们无法用RelativeLayout去实现,如下分析

  • B在A下方,通过 android:layout_below="@+id/txt_a"实现
  • B要在A底部边框上,垂直居中。那么我们要知道B的height。B再使用marginTop="-height/2"才能达到效果。所以在xml里无法实现,只能去代码里动态计算。

使用ConstraintLayout则可轻松完成:

<androidx.constraintlayout.widget.ConstraintLayout ...>


    <TextView
        android:id="@+id/txt_a"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        .../>

    <TextView
        app:layout_constraintRight_toRightOf="@+id/txt_a"
        app:layout_constraintLeft_toLeftOf="@+id/txt_a"
        app:layout_constraintTop_toBottomOf="@+id/txt_a"
        app:layout_constraintBottom_toBottomOf="@+id/txt_a"
        android:id="@+id/txt_b"
        .../>

</androidx.constraintlayout.widget.ConstraintLayout>
复制代码

上面只是简单的2个小例子,实战上在复杂布局里,真的有太多好处。接下我们具体说说把。

二、ConstraintLayout各属性介绍

2.1、相对定位

2.1.1、例1,如图:靠右边

实现如图功能:

<TextView
    ...
    app:layout_constraintRight_toRightOf="parent"
    />
复制代码

layout_constraintRight_toRightOf 属性还有left、right、top、bottom、start、end等搭配使用。什么意思呢?

  • 第一个Right代表 本控件的右边
  • 第二个Right代表 要位于目标控件右边。
  • 所以app:layout_constraintRight_toRightOf=“parent”; 红色A的右边位于父容器的右边

注意start、end是因为很多国家阅读习惯不一样,比如我们是习惯从左往右阅读。所以start对于我们来说就是left,end就是right。


2.1.2、例2,如图:居中

实现如图功能:

<TextView
    ...
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    />
复制代码

右边和父控件右边对齐,左边和父控件左边对齐。这样就可以达到横向居中,同理竖直。

注意这里居中了。同样还可以用margin进行偏移。也可以利用属性app:layout_constraintHorizontal_bias=“0.5”,进行横向偏移,0.5意思也还算居中。同理竖直的属性。


2.1.3、例3,如图:充满屏幕

比如B要在A的右边,且相对A上下居中,而且B要充满剩余横屏:

<TextView
    app:layout_constraintLeft_toRightOf="@+id/txt_a"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/txt_a"
    app:layout_constraintBottom_toBottomOf="@id/txt_a"
    android:layout_width="0dp"
    ...
    />
复制代码

要想充满全屏把宽度设置为0dp。然后通过以下代码:

  • app:layout_constraintLeft_toRightOf=“@+id/txt_a”;B的左边和A的右边对齐
  • app:layout_constraintRight_toRightOf=“parent”;B的右边和父容器的右边对齐

这样B就在A的右边,且横向充满屏。注意:在约束布局里match_parent是不生效的。

要想B竖直方向与A平行的话,通过如下代码:

  • app:layout_constraintTop_toTopOf=“@+id/txt_a”;B的上边和A的上边对齐
  • app:layout_constraintBottom_toBottomOf=“@id/txt_a”;B的下边和A的下边对齐

这样就可以让B在A右边,上下居中。


2.1.4、例4,如图:不嵌套居中

在列表里我们经常会遇到这样的需求,A是一张图片,title是标题,des是描述。用RelativeLayout的做法是:继续用一个layout布局包裹title和des,居中然后放在A的右边。

用ConstraintLayout的话不用嵌套,3个view就搞定了。如下:

<ImageView
    ...
    />


<TextView
    app:layout_constraintTop_toTopOf="@+id/image"
    app:layout_constraintBottom_toTopOf="@+id/txt_des"
    android:id="@+id/txt_title"
    ...
    />

<TextView
    app:layout_constraintTop_toBottomOf="@+id/txt_title"
    app:layout_constraintBottom_toBottomOf="@+id/image"
    
   * 这个时候你会发现还没有居中因为还缺了一条约束,title在des上边;
    android:id="@+id/txt_des"
    ...
    />
复制代码

因为例3,已经讲过如何全屏了,这里主要讲title和des如何居中:

  • des 在title下面;app:layout_constraintTop_toBottomOf=“@+id/txt_title”
  • title上边和图片A上边对齐;app:layout_constraintTop_toTopOf=“@+id/image”
  • des下边和图片A下边对齐;app:layout_constraintBottom_toBottomOf=“@+id/image”
  • 这个时候你会发现还没有居中因为还缺了一条约束,title在des上边; app:layout_constraintBottom_toTopOf=“@+id/txt_des”,这和RelativeLayout有些区别

这里还有一个属性文本基线对齐,和RelativeLayout里的 layout_alignBaseline 是一样的效果

  • app:layout_constraintBaseline_toBaselineOf=“@+id/txt_a”

关于相对定位已经讲完了。

2.2、角度定位

如图:

实现代码如下:

<TextView
    android:id="@+id/txt_a"
    ...
     />

<TextView
    app:layout_constraintCircle="@+id/txt_a";
    app:layout_constraintCircleAngle="90"
    app:layout_constraintCircleRadius="100dp"
    android:id="@+id/txt_b"
    ...
    />
复制代码
  • app:layout_constraintCircle=“@+id/txt_a”;B相对于A角度定位
  • app:layout_constraintCircleAngle=“90”;角度定位角度为90°
  • app:layout_constraintCircleRadius=“100dp”;B中心与A中心相差100dp

这个时候我也在想有什么用呢?我觉得可以用在某些自定义view上。比如圆形menu、实现时钟更简单了。我用属性动画,实现一个效果你就明白了。录制有点卡顿,真机不会…

2.3、边距

2.3.1、margin

margin 值要生效,一定是伴随约束属性的。什么意思呢,要实现如图功能:

代码实现如下:

<TextView
    android:id="@+id/txt_a"
    ...
     />

<TextView
    android:layout_marginLeft="20dp"
    app:layout_constraintLeft_toRightOf="@+id/txt_a"
    android:id="@+id/txt_b"
    ...
     />
复制代码
  • 比如B左边和A的右边对齐app:layout_constraintLeft_toRightOf=“@+id/txt_a”,此时在B上加上margin值生效
  • 如果这个时候在A上加上android:layout_marginRight=“50dp”,是不生效的。谨记

2.3.2、goneMargin

goneMargin属性比较有意思。比如要实现如下功能,从图1–>图2:

图1如下:

图2如下:

隐藏A的时候B要置顶排布。在RelativeLayout里,我们只需要在A里使用Margin_Bottom=“20dp”,然后对A进行隐藏就可以实现了。但是在ConstraintLayout里,因为margin生效必须要有约束,所以这个时候B就要使用goneMargin属性,实现代码如下:

<TextView
    android:id="@+id/txt_a"
    android:visibility="gone"
    ...
     />

//意思就是当有约束的控件隐藏时,goneMargin就会生效,那么B就会置顶排布。
<TextView
    android:id="@+id/txt_b"
    app:layout_constraintTop_toBottomOf="@+id/txt_a"
    android:layout_marginTop="20dp"
    app:layout_goneMarginTop="0dp"
    ...
     />
复制代码

2.4、尺寸

在ConstraintLayout里,设置尺寸大小的有3个:

  • 设置固定的dp值
  • wrap_content
  • 0dp(MATCH_CONSTRAINT),前面讲过match_parent是不生效的。如果想达到match_parent的效果只能通过0dp和约束来达到。

2.4.1、满屏

比如控件A满屏(在2.1.3一样),如下效果:

则通过如下代码实现:

<TextView
    android:layout_width="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    ...
    />
复制代码

2.4.2、layout_constraintWidth_max

属性 layout_constraintWidth_max 在使用dp固定值的时候和 android:maxWidth=“100dp” 用法是一致的。

但是layout_constraintWidth_max可以搭配属性 app:layout_constraintWidth_percent=“0.5” 使用,什么意思呢。

比如我们要实现红色区域占父容器宽度的一半:

实现代码如下:

<TextView
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_width="0dp"
    android:text=""
    app:layout_constraintWidth_max="wrap"
    app:layout_constraintWidth_percent="0.5"
    ...
     />
复制代码

这里这样设置的时候,意思最大宽度是父容器的一半。注意这里text=""空的时候,会像图中直接展示父容器的一半,假设我们给text="A"设置上值后,如下图,那么只会展示A的宽度,但最大宽度是父容器一半。


2.4.3、app:layout_constrainedWidth

app:layout_constrainedWidth=“true” 属性,是按约束去限制宽度,什么意思呢?我们先不加上这条属性,实现如下图效果是这样的:

代码是这样的:

<TextView
    android:id="@+id/txt_a"
    ...
     />


<TextView
    app:layout_constraintLeft_toLeftOf="@id/txt_a"
    app:layout_constraintRight_toRightOf="@id/txt_a"
    app:layout_constraintTop_toBottomOf="@+id/txt_a"
    android:id="@+id/txt_b"
    android:layout_width="wrap_content"
    android:text="BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
    ...
     />
复制代码

给txt_b加上 app:layout_constrainedWidth=“true” 属性后,就会实现下面的效果:

意思就是B的最大宽度按照A的宽度来约束,这里要注意的是,B的宽度是wrap_content。假设这里是0dp的话,那这里就另外一种情况了,意思就是B的宽度和A的宽度一样宽。


2.4.4、layout_constraintDimensionRatio 宽高比

app:layout_constraintDimensionRatio=“1:1”,宽高比,注意这一条属性要生效的话要达到2个条件:

  • 其中一条边为0dp
  • 其中一条边为固定值或wrap_content

比如要实现如下,一个正方形的A:

代码如下:

<TextView
    android:id="@+id/txt_a"
    android:layout_width="0dp"
    android:layout_height="50dp"
    app:layout_constraintDimensionRatio="1:1"
    ...
    />
复制代码

2.5、链/约束.

指多个控件在一条方向上相互约束,形成一条链子似的。(这里还不算是线性布局, 因为还没加权重) 比如实现如下:

代码如下:

<TextView
    android:id="@+id/txt_1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/txt_2"
    ...
    />

<TextView
    android:id="@+id/txt_2"
    app:layout_constraintLeft_toRightOf="@+id/txt_1"
    app:layout_constraintRight_toLeftOf="@id/txt_3" 
    ...
    />

<TextView
    android:id="@+id/txt_3"
    app:layout_constraintLeft_toRightOf="@id/txt_2"
    app:layout_constraintRight_toRightOf="parent"
    ...
    />
复制代码

这样就在横向方向形成一条链子了(同理纵向),在一条链子的第一个控件也就是链头,我们可以加上layout_constraintHorizontal_chainStyle来改变整条链的样式,有3中

  • spread就是上面的样子
  • spread_inside

  • packed


上面的降的宽度都是wrap_content,如果我们把宽度设置为0do然后通过属性layout_constraintHorizontal_weight(同理纵向),加上权重后,就是我们的线性布局。比如实现如下:

代码:

<TextView
    android:id="@+id/txt_1"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="2"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/txt_2"
    ...
    />

<TextView
    android:id="@+id/txt_2"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toRightOf="@+id/txt_1"
    app:layout_constraintRight_toLeftOf="@id/txt_3"
    ...
    />

<TextView
    android:id="@+id/txt_3"
    android:layout_width="0dp"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toRightOf="@id/txt_2"
    app:layout_constraintRight_toRightOf="parent"
    ...
    />
复制代码

2.6、辅助工具

2.6.1、Guideline

guideline和辅助先一样,运行项目的时候,不会显示在界面上。也不用担心会消耗性能,因为在其onDraw方法里,没有具体的实现。guideline属性具体如下:

  • android:orientation=“vertical” 纵向辅助线,同理横向
  • 然后就是确定辅助线的位置,通过下面3个属性,多个出现时只会有一个生效,权重是百分比 > begin > end

app:layout_constraintGuide_percent=“0.5” 在父布局的百分比位置
app:layout_constraintGuide_begin=“10dp” 距离父布局开始位置,横向为左,纵向为顶部
app:layout_constraintGuide_end=“10dp” 距离父布局末尾位置

比如我们之前经常会写 时间轴 类似的控件。就可以用这个guideline辅助完成。

2.6.2、Barrier

假设有3个控件A、B、C,如果我们A,B的宽度不固定,同时又希望C在A,B的右边。如果用一个布局把A,B包裹起来,然后让C在A,B右边可以实现,如果不像嵌套就要通过Barrier了,如下图:

实现代码如下:

<TextView
    android:id="@+id/txt_1"
    ...
    />

<TextView
    android:id="@+id/txt_2"
    app:layout_constraintTop_toBottomOf="@+id/txt_1"
    ...
    />

<androidx.constraintlayout.widget.Barrier
    android:id="@+id/barrier"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:barrierDirection="right"
    app:constraint_referenced_ids="txt_1,txt_2" />

<TextView
    android:id="@+id/txt_3"
    app:layout_constraintLeft_toRightOf="@+id/barrier"
    ...
    />
复制代码

Barrier属性有:

  • app:barrierDirection=“right” 为屏障时,哪个方向的屏障,图中是A和B的右边
  • app:constraint_referenced_ids=“txt_1,txt_2” 为屏障引用的id,用逗号隔开

注意,这里是为减少布局嵌套。必要时我觉得可以灵活运用嵌套。

2.6.3、Group

被布局Layout嵌套的控件A,B,我们要隐藏他只需要隐藏这个嵌套布局Layout即可。在ConstraintLayout里,没有了布局嵌套隐藏A,B就通过Group实现:

<androidx.constraintlayout.widget.Group
    android:visibility="gone"
    android:id="@+id/group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:constraint_referenced_ids="txt_1,txt_2"
    />
复制代码

2.6.4、Placeholder:小重点。

网上很多资料对这Placeholder都一比带过。看了我这里的介绍,你会更清楚怎么使用Placeholder。

Placeholder从名字看,就是占位的意思。你可以在一个页面设置多个不同位置的占位。然后通过代码setContentId直接可以改变某个view移动到我们的占位图里。什么意思呢。请看下图:

我在界面上设置了2个Placeholder。通过2个按钮,控制蓝色TextView处于在哪个PlaceHolder里。

  • 首先通过app:content=“@+id/txt_blue”,让蓝色textView默认在placeholder_1的位置。
  • 注意蓝色textView还是需要在xml里的。
<TextView
    android:id="@+id/txt_red"
    ...
     />


<androidx.constraintlayout.widget.Placeholder
    android:id="@+id/placeholder_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txt_1"
    app:content="@+id/txt_blue"
    />

<TextView
    android:id="@+id/txt_green"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/placeholder_1"
    ...
    />

<androidx.constraintlayout.widget.Placeholder
    android:id="@+id/placeholder_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<TextView
    android:id="@+id/txt_blue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
     />
复制代码

2个按钮我没写上去懂的都懂,那2个按钮上的代码呢?注意看了。这可能是google还没有解决这话bug。下面我来说下

  • 当点击按钮“在位置2里”,把蓝色textView放进placeholde_2,通过placeholde_2.setContentId(R.id.txt_2);
  • 注意此时placeholde_1还是占据着蓝色textView的高度。
  • 当点击“在位置1里”,我们首先要把placeholde_2里蓝色TextView的引用清空掉,用placeholde_2.setContentId(-1);
  • 因为placeholde_1有之前蓝色TextView的引用,虽然是同一个东西,但是设置会不生效。必须清楚掉之前的引用(看源码得出来的结果)。通过placeholde_1.setContentId(-1);
  • 最后通过placeholde_1.setContentId(R.id.txt_2);就可以使蓝色textView又回到了placeholde_1身上了

具体代码如下:

findViewById(R.id.buttonPanel).setOnClickListener(v ->
    placeholde_2.setContentId(R.id.txt_2);
);


findViewById(R.id.buttonPane2).setOnClickListener(v ->
    placeholde_2.setContentId(-1);
    placeholde_1.setContentId(-1);
    placeholde_1.setContentId(R.id.txt_2);
);
复制代码

如果这个时候,我们想只要Placeholder移除了蓝色TextView。我们就让Placeholder也隐藏不可见。其实是有办法的。

  • 步骤1:给Placeholder加上属性app:placeholder_emptyVisibility=“gone”。
  • 步骤2:除了以上步骤,我们还要搭配setContentId(-1),才可以实现效果。

看效果:

代码实现如下:

findViewById(R.id.buttonPanel).setOnClickListener(v ->
    placeholde_1.setContentId(-1);
    placeholde_2.setContentId(R.id.txt_2);
);


findViewById(R.id.buttonPane2).setOnClickListener(v ->
    placeholde_2.setContentId(-1);
    placeholde_1.setContentId(R.id.txt_2);
);
复制代码

2.6.5、Layer

Layer什么效果呢。比如A B C在如图排布,想在想要有一个黑色背景包裹A,B,C,达到如图效果:

实现如下:

<androidx.constraintlayout.helper.widget.Layer
    android:id="@+id/layer"
    android:background="#000"
    app:constraint_referenced_ids="txt_a,txt_b,txt_c"
    android:padding="15dp"
    ...
    />


<TextView
    android:id="@+id/txt_a"
    ...
    />


<TextView
    android:id="@+id/txt_b"
    ...
    />


<TextView
    android:id="@+id/txt_c"
    ...
    />
复制代码

经过我实际测试,其实Layer能完全代替的了2.6.3的Group,但是不能实现Barrier的功能。

2.6.6、Flow

直接看图把,简单实现这种网格布局:

代码如下:

<androidx.constraintlayout.helper.widget.Flow
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    appconstraintlayout使用详解,减少嵌套ui,提升性能(代码片段)

...G学习资料和大厂面经前言对于初学者来说,可能觉得ConstraintLayout属性多,且属性长而弃用它,那你错失了这个大宝贝。因为在复杂布局,我们会一直用RelativeLayout和LinearLayout去嵌套,因为嵌套的ViewGroup会导致... 查看详情

constraintlayout

ConstraintLayout使用笔记具体使用参考:http://blog.csdn.net/guolin_blog/article/details/53122387ConstraintLayout好处还是很明显,确实可以减少嵌套。性参对比参阅:http://www.cnblogs.com/liujingg/p/7161319.html简单嵌套ConstraintLayout性能比较差。 查看详情

android新控件之constraintlayout(约束布局)

参考技术AConstraintLayout(约束布局)继承于ViewGroup允许开发者以灵活的方式定位和调整小部件的大小ConstraintLayout可让开发者使用扁平视图层次结构(无嵌套视图组)创建复杂的大型布局。它与RelativeLayout相似,其中所有的视图均根... 查看详情

android开发constraintlayout布局的详解(代码片段)

...udio快速拖拉组件来实现布局,但是前期建议好好了解ConstraintLayout的属性  3.View 查看详情

ConstraintLayout 是不是仍然优于 Jetpack Compose 中的嵌套列和行?

】ConstraintLayout是不是仍然优于JetpackCompose中的嵌套列和行?【英文标题】:IsConstraintLayoutstillpreferredovernestedColumn&RowinJetpackCompose?ConstraintLayout是否仍然优于JetpackCompose中的嵌套列和行?【发布时间】:2021-05-0912:24:44【问题描述... 查看详情

constraintlayout属性详解和chain的使用(代码片段)

本篇文章已授权微信公众号guolin_blog(郭霖)独家发布想看我更多文章:【张旭童的博客】http://blog.csdn.net/zxt0601想来gayhub和我gaygayup:【mcxtzhang的Github主页】https://github.com/mcxtzhang概述小伙伴们好久不见,我又回来啦。说... 查看详情

flutter嵌套地狱?不存在的,constraintlayout来解救!

Flutter嵌套地狱?不存在的,ConstraintLayout来解救!https://github.com/hackware1993/Flutter_ConstraintLayout没用过,但是看着听可以的! 查看详情

constraintlayout简明实用指南(代码片段)

一、为什么要用ConstraintLayout当布局嵌套深入比较深的时候,往往会伴随着一些性能问题。所以很多时候我们建议使用RelativeLayout或者GridLayout来简化掉布局的深度。而对于简化布局深度,ConstraintLayout几乎可以做到极致。具... 查看详情

android:constraintlayout偏移(bias)计算方式详解(代码片段)

ConstraintLayoutBias1、Bias定义,水平偏移和垂直偏移1、水平偏移(app:layout_constraintHorizontal_bias):2、垂直偏移(app:layout_constraintVertical_bias)2、使用参考1、Bias定义,水平偏移和垂直偏移注意前提:该属性生效的前提是,必须设置水平... 查看详情

Android:ScrollView包含两个嵌套的ConstraintLayout,无法水平填满屏幕

】Android:ScrollView包含两个嵌套的ConstraintLayout,无法水平填满屏幕【英文标题】:Android:ScrollViewcontainingtwonestedConstraintLayouts,cantfillscreenhorizontally【发布时间】:2020-11-1916:20:27【问题描述】:我遇到以下XML问题:<?xmlversion="1.0"enc... 查看详情

安卓基础使用guideline与约束辅助布局的平分空间设计(代码片段)

ConstraintLayout布局已经推出了很长一段时间,功能也是比较强大,能有效减少界面的视图层级嵌套,一定程度提升界面绘制效率。 在项目中,我也是最近才选择开始使用ConstraintLayout,之前一直用的是LinearLayout+FrameLayout进行复... 查看详情

constraintlayout约束布局基础使用(代码片段)

...需求,google为了更好的解决适配问题,提供出来ConstraintLayout约束布局。为什么要约束布局?在ConstraintLayout之前,AndroidUI是由线性布局、FrameLayout和RelativeLayout等几个ViewGroup创建的。在Android开发历史中,这些布... 查看详情

使用constraintlayout构建响应式ui(buildaresponsiveuiwithconstraintlayout)

原文:https://weiyf.cn/2017/03/10/Build%20a%20Responsive%20UI%20with%20ConstraintLayout/原文:http://www.jianshu.com/p/f61227a2775f 简介:ConstraintLayout不需要使用嵌套布局就可以让我们去构建一个大而复杂的布局,他与RelativeLayout很相似,所有 查看详情

constraintlayout2.0一篇写不完之嵌套滚动怎么滚

点击上方蓝字关注我,知识会给你力量在ConstraintLayout1.x阶段,它主要提供的能力是对静态布局的支撑,那么到2.x之后,MotionLayout的拓展,让它对动态布局的支持有了进一步的优化,在1.x阶段不能实现的嵌... 查看详情

constraintlayout简明实用指南(代码片段)

一、为什么要用ConstraintLayout当布局嵌套深入比较深的时候,往往会伴随着一些性能问题。所以很多时候我们建议使用RelativeLayout或者GridLayout来简化掉布局的深度。而对于简化布局深度,ConstraintLayout几乎可以做到极致。具... 查看详情

寻找更简洁的声明方式来减少使用扩展运算符的嵌套

】寻找更简洁的声明方式来减少使用扩展运算符的嵌套【英文标题】:Lookingforcleanerdeclarativewaytoreducenestingwithspreadoperator【发布时间】:2020-04-1516:18:07【问题描述】:当只有一个项目发生变化时,我不喜欢使用映射和循环从JavaScri... 查看详情

android开发constraintlayout详解(代码片段)

app:layout_constraintHorizontal_chainStyle app:layout_constraintDimensionRatio<Buttonandroid:layout_width="0dp"android:layout_height="0dp"app:layout_constraintDimensionRatio="H,16:9"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"/>app:layout_constr... 查看详情

android启动优化之布局优化

1.使用ConstraintLayout减少嵌套,使用inclue,merge,viewstub等2.使用异步加载布局,AsyncLayoutInfater。//子线程异步执行xml加载newAsyncLayoutInflater().inflate(R.layout.activity_main.xml,null,newAsyncLayoutInflater.onInflateFinished()@overridepublicvoidon... 查看详情