ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ConstrainLayout 基础教程3,2018android面试题

2021-12-29 16:33:34  阅读:213  来源: 互联网

标签:面试题 layout app ConstrainLayout ConstraintLayout 基础教程 android com id


<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" />

<ImageButton
android:id="@+id/favorite"
android:layout_width=“48dp”
android:layout_height=“48dp”
android:layout_marginTop=“16dp”
android:background="#00000000"
android:scaleType=“centerInside”
android:src="@drawable/favorite"
android:tint="#E64A19"
app:layout_constraintEnd_toStartOf="@id/mail"
app:layout_constraintStart_toStartOf="paren

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享

t"
app:layout_constraintTop_toTopOf=“parent” />

</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:优化尺寸测量(实验)。

在设置值时,可以设置多个,如:

app:layout_optimizationLevel=“direct|barrier|dimensions”

可视化编辑器

这部分可以参考以下文章

使用布局编辑器构建界面

[译文]使用ConstraintLayout构建一个响应式的UI

自律给你自由——设计布局的新姿势

Android新特性介绍,ConstraintLayout完全解析

未来布局之星——ConstraintLayout

demo

quanqi.org/2016/05/20/…

juejin.im/post/592655…

juejin.im/post/5c74f2…

www.jianshu.com/p/b3cd72524…

动画

juejin.im/post/59258f…

www.jianshu.com/p/9c7503f47…

高级

developer.android.com/reference/a…

developer.android.com/reference/a…

Barriers clone() in ConstraintSet 1.1.3

约束集与动画

您可以将 ConstraintLayout 随同 ConstraintSet (约束集)一起使用来一次实现多个元素的动画效果。

一个 ConstraintSet 仅持有一个 ConstraintLayout 的约束。你可以在代码中创建一个ConstraintSet,或者从一个布局文件中加载它。然后,您可以将 ConstraintSet 应用于 ConstraintLayout,更新所有约束以匹配 ConstraintSet 中的约束。

要使其具有动画效果,请使用 support library 中的 TransitionManager.beginDelayedTransition() 方法。此功能将使您的 ConstraintSet 中的所有布局的更新都通过动画来呈现。

这是一个更深入地涵盖了这个话题的视频:

motionlaytout

github.com/googlesampl…

性能分析

mp.weixin.qq.com/s/gGR2itbY7…

最后

其实要轻松掌握很简单,要点就两个:

  1. 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
  2. 多练。 (视频优势是互动感强,容易集中注意力)

你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。

阿里P7Android高级教程

下面资料部分截图,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

附送高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、近期面试跳槽、自身职业规划迷茫的朋友们。

Android核心高级技术PDF资料,BAT大厂面试真题解析;

摸索成长,不成体系的学习效果低效漫长且无助。

阿里P7Android高级教程

下面资料部分截图,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

[外链图片转存中…(img-IoZ6YDzH-1640765881914)]

附送高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、近期面试跳槽、自身职业规划迷茫的朋友们。

Android核心高级技术PDF资料,BAT大厂面试真题解析;
[外链图片转存中…(img-xiNQSoG3-1640765881914)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

标签:面试题,layout,app,ConstrainLayout,ConstraintLayout,基础教程,android,com,id
来源: https://blog.csdn.net/m0_65689954/article/details/122218628

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有