最近一个礼拜业余时间都在搞我的淘宝店铺,有兴趣的都可搜一下:吉安车品,主营汽车脚垫/后备箱垫/座垫,还在打基础阶段,不急,慢慢搞,当业余爱好去买卖;
吃饭手艺功夫自然不能丢,为下个月的变数准备,今天开始复习/学习一些基础内容,今天的笔记是ConstraintLayout的几个属性。
1、Circular positioning(圆形定位)
标题后面的中文是自己翻译的,可能不是很准确。
官方文档是这么介绍的:
You can constrain a widget center relative to another widget center, at an angle and a distance. This allows you to position a widget on a circle
我是这么理解的,您可以将一个控件的中心以一定的角度和距离约束到另一个控件的中心,相当于在一个圆上放置一个控件。
示例代码如下:
<Button android:id="@+id/buttonA" ... />
<Button android:id="@+id/buttonB" ...
//引用的控件ID
app:layout_constraintCircle="@+id/buttonA"
//圆半径
app:layout_constraintCircleRadius="100dp"
//偏移圆角度 水平右方向为0逆时针方向旋转
app:layout_constraintCircleAngle="45" />
效果图:
图文并茂,理解起来较容易些。圆形定位使用其他布局是很难实现的(除自定义外),该功能在实际的开发中用的并不多,可以用来实现类似钟表的效果。该功能只不过是约束布局的冰山一角,且往下看。
2、WRAP_CONTENT : enforcing constraints(强制约束)
官方文档是这么介绍的:
If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute
英文一直是我的弱项,我是这么理解的,1.1.0 版本之前是没有这个功能的,说的是控件的宽设置为 WRAP_CONTENT
(包裹内容)时,如果实际宽度超过了约束的最大宽度,那么约束会失效(高同理),为了防止约束失效,增加了以下属性:
- app:layout_constrainedWidth=”true|false” //默认false
- app:layout_constrainedHeight=”true|false” //默认false
官网并没有过多说明,那么怎么去理解呢,接下来以app:layout_constrainedWidth
属性来看两个例子。
a、例子
B
控件位于 A
控件右侧与屏幕右侧的中间。代码如下:
<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="A"
app:layout_constraintLeft_toLeftOf="parent"/>
<Button