ConstrainLayout 基础教程3,跳槽薪资翻倍

xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>

<androidx.constraintlayout.widget.Barrier
android:id=“@+id/barrier7”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
app:barrierDirection=“end”
app:constraint_referenced_ids=“textView2,textView1” />

</androidx.constraintlayout.widget.ConstraintLayout>

barrierAllowsGoneWidgets

Barrier-barrierAllowsGoneWidgets.gif

目前button3和button1 button2的top对齐,此时如果button1 Gone了呢?

如上图效果,button1 gone之后,会变为一个点,所以button3顶齐父布局也没问题。但有的时候这不符合我们的需求,我们希望Barrier不要关注Gone的View了,所以谷歌提供了属性barrierAllowsGoneWidgets,设为false后,就不在关注Gone的View了,效果如上图,button1 gone之后,button3 不再和父布局顶齐,而是和button2顶齐。

Barrier特别的地方就在于Barrier元素自身。app:barrierDirection 属性决定 Barrier 的方向 - 这里把它放在被引用view的后面。被引用的view 是布局中的view的id列表,用逗号隔开。

借用一张图 来自medium.com/androiddeve…

Barrier-demo.gif

Group

使用组,您可以将某些视图分组在一起。不要把这与Android中的普通ViewGroups混淆。ConstraintLayout中的一个组仅包含对视图ID的引用,而不将组合中的视图嵌套。这样一来,您可以设置组中控件的可见性仅通过设置组的可见性就行了,而无需设置每个视图的可见性。这对于诸如错误屏幕或加载屏幕的事情是有用的,其中一些元素需要一次更改其可见性

其可使用到的属性为:

  • constraint_referenced_ids:指定所引用控件的 id。

<androidx.constraintlayout.widget.Group
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:visibility=“gone”
app:constraint_referenced_ids=“title, desc” />

如果有多个 Group,是可以同时指定相同的控件的,最终是以 XML 中最后声明的 Group 为准。

Placeholder

Placeholder顾名思义,就是用来一个占位的东西,它可以通过 setContentId() 方法将占位符变为有效的视图。如果视图已经存在于屏幕上,那么视图将会从原有位置消失。

除此之外,还可以通过 setEmptyVisibility() 方法设置当视图不存在时占位符的可见性。

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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:id=“@+id/constraintLayout”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>

<androidx.constraintlayout.widget.Placeholder
android:id=“@+id/placeholder”
android:layout_width=“96dp”
android:layout_height=“96dp”
android:layout_marginStart=“8dp”
android:layout_marginTop=“8dp”
android:layout_marginEnd=“8dp”
android:layout_marginBottom=“8dp”
android:scaleType=“centerInside”
app:layout_constraintBottom_toBottomOf=“parent”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“@+id/mail” />

</androidx.constraintlayout.widget.ConstraintLayout>

class DemoFragment : Fragment(), View.OnClickListener {
override fun onClick(v: View) {
//call TransitionManager so and pass ConstraintLayout to perform smooth animation
TransitionManager.beginDelayedTransition(mConstraintLayout);
//finally set clicked view at placeholder
mPlaceholder.setContentId(v.id)

}

private lateinit var dashboardViewModel: DashboardViewModel
private lateinit var mConstraintLayout: ConstraintLayout
private lateinit var mPlaceholder: Placeholder

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)

val favorite: ImageButton = root.findViewById(R.id.favorite)
favorite.setOnClickListener(this)
val star : ImageButton = root.findViewById(R.id.star)
star.setOnClickListener(this)
val checked : ImageButton = root.findViewById(R.id.checked)
checked.setOnClickListener(this)
val delete : ImageButton = root.findViewById(R.id.delete)
delete.setOnClickListener(this)

mConstraintLayout = root.findViewById(R.id.constraintLayout)
mPlaceholder = root.findViewById(R.id.placeholder)

return root
}
}

Placeholder-demo.gif

Optimizer (优化器)

需要知道的是,当我们使用 MATCH_CONSTRAINT 时,ConstraintLayout 将不得不对控件进行 2 次测量,而测量的操作是昂贵的。

而优化器(Optimizer)的作用就是对 ConstraintLayout 进行优化,对应设置给 ConstraintLauyout 的属性是:

  • layout_optimizationLevel。

可设置的值有:

  • none:不应用优化。
  • standard:仅优化直接约束和屏障约束(默认的)。
  • direct:优化直接约束。
  • barrier:优化屏障约束。
  • chain:优化链约束(实验)。
  • dimensions:优化尺寸测量(实验)。

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

结尾

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~

规划。

这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~

[外链图片转存中…(img-Goxim5U6-1710656631457)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值