Android ConstraintLayout按比例缩放View
关键点有两个,第一,使用Android ConstraintLayout的layout_constraintDimensionRatio属性,设置宽高比缩放比例,宽:高。第二,恒定宽和高,另外一个方向就会按照比例缩放。
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="@+id/text0"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="w=0,h=100\n5:2"
android:textColor="@android:color/white"
android:textSize="10dp"
app:layout_constraintDimensionRatio="5:2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text1"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="@android:color/holo_red_light"
android:gravity="center"
android:text="w=100,h=0\n5:2"
android:textColor="@android:color/white"
android:textSize="10dp"
app:layout_constraintDimensionRatio="5:2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/text0" />
</android.support.constraint.ConstraintLayout>
运行结果如图:
小菜最近在学习 ViewPager 的小动画,说来惭愧,工作这么久了一直没有认真了解过动画这部分,今天特意学习一下 Android 的基本动画。
Android 的基本的动画包括 alpha(透明度)/ scale(缩放)/ translate(位移) / rotate(旋转)四种,小菜今天学习一下 scale 渐变缩放动画效果。
Android开发 - 掌握ConstraintLayout(七)辅助线(Guideline)
了解过UI设计的同学都知道,在设计的时候,我们经常在界面上拖进一些辅助线来帮我们对齐UI元素,或者方便我们统一的页边距。
在ConstraintLayout的编辑器中,同样也支持这样的功能,我们可以创建一些横向的或者纵向的Guideline,在布局界面的时候可以充分利用这些辅助线,对齐我们的View,避免重复写一些marginXXX。
我们选中三个按钮后在上面点右键创建链条:
创建后我们发现这三个View平均分布地排列了:
最简单的使用是平均分布,当然也可以不平均分布,具体看约束的具体设置,比如将第一个Button的marginEnd设置成10后链条会自动地分布每个View的位置。
比如实现这样一个场景:
"在屏幕宽度的1/4的地方放置一个View"
使用传统布局时,实现按照屏幕的宽度(高度),或者相对两个View之间距离的一个比例来进行布局,就显得非常麻烦,但是当使用ConstraintLayout时,就可以很简单地实现这样的需求。
Android开发 - 掌握ConstraintLayout(三)编辑器
从本篇博客开始我们开始介绍如何使用ConstraintLayout。既然ConstraintLayout叫约束布局,首先我们先介绍什么叫约束(Constraints):
约束(Constraints)
一个约束表示View之间的"布局约束"关系,以及约束的位置,类似RelativeLayout的"相对"概念。
ConstraintLayout是在2016的Google I/O大会上发布的,经过这么长时间的更新,现在已经非常稳定。
支持Android 2.3(API 9)+
目前的Android设置几乎没有低于Android4.4(Api 19)的,所以ConstraintLayout可以支持所有的设备。