ConstraintLayout 水平比例布局 1:1:2
//以下代码展示了一个 1:1:2的ConstraintLayout布局, 关键点就是 位置的left,right的摆放,才能出现这个效果。 认真体会
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.okaylens.it8951demo.MainActivity">
<Button
android:id="@+id/connect_usb_bt"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@id/send_bmp_bt"
android:layout_width="0dp"
android:layout_height="100dp"
<Button
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/send_bmp_bt"
app:layout_constraintLeft_toRightOf="@id/connect_usb_bt"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@id/send_jpg_bt"
android:layout_width="0dp"
android:layout_height="100dp"
<Button
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/send_jpg_bt"
app:layout_constraintLeft_toRightOf="@id/send_bmp_bt"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="2"
android:layout_width="0dp"
android:layout_height="100dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
[DEPRECATED in support lib v26] you should now use ConstraintLayout widget https://developer.android.com/training/constraint-layout/index.html
Android Percent Support Lib Sample :triangular_ruler::triangular_ruler::triangular_ruler:
I made a sample of the new percent support library.
You can check official docs reference here
and here.
This library provide percentage based layouts,
horizontal and vertical at the same time.
simple result
complex result
How to use :
just add percent support li
接着上一篇文章讲
ConstraintLayout属性之Group,GuideLine(三)
先前的几篇文章只是讲了一下ConstraintLayout的基本属性,都很简单,感觉没有眼前一亮的感觉,接下来要讲的东西也很简单,但是会很实用,尤其是在屏幕适配方面,如果运用的灵活,简直美滋滋。
在平时写布局的时候我们通常都是以dp为单位的,这也是google推崇的一种方式,但是dp并不能完全适配所有的手...
<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:layout_width="match_pa..
为了创建比例大小的子View,可以将LinearLayout的宽度和高度设为fill_parent, 而将子View的宽度或是高度设为0,然后为子View设置不同权重(weight) ,这样子View的大小就会权值成比例。
本例使用横向LinearLayout,LinearLayout的android:layout_width=”match_parent”,表示将使用整个屏幕宽度。
对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie
有时候在布局界面的时候,UI要求某个View或者某张图片按比例显示,以适应不同的屏幕分辨率。
通常我们时通过自定义View或者引入第三方的库来解决。现在我们既然已经使用了ConstraintLayout,它本身就支持这样的按比例设置View大小的功能。
下面我们来介绍如何使用:
首先我们在布局中添加一个View:
此时,没有添加任何约束,显示的比例就是原始图片的比例。
添加水平方向的约束:...
一个复杂的布局或自定义View如何在添加到其他不同大小的ViewGroup中按比例去缩放自己的布局内容呢?我尝试使用ConstraintLayout解决了这个问题。
1. 简单的布局
大家先看一个简单的布局,由上下两个view组成,都是16:9的比例。
左边是设置android:layout_width="match_parent"的情况,右边是将layout_width设为了200dp,模拟缩小到宽为200dp的View。大家可以发现的是他们实现等比缩小了。实现原理也很简单,就是通过ConstraintL
ConstraintLayout的尺寸 dimensions
有时候,我们需要创建一些固定方向比的 View 组件,最常使用固定横纵比的就是当 ImageView 用于展示一些固定横纵比的图片的时候。举些例子,书面封面(尺寸横纵比多种多样),电影海报(一般是 4:6 ),电影剧照(一般是 1.85:1 或 2.39:1 ),电视剧(一般是...
2.ConstraintLayout没有嵌套布局不能使用match_parent
约束布局ConstraintLayout 是一个ViewGroup,可以在Api9以上的Android系统使用它,它的出现主要是为了解决布局嵌套过多的问题,以灵活的方式定位和调整小部件
2.为什么要用ConstraintLayout
在开发...
通过布局就可以解决,下面贴出全部布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schema...
Android ConstraintLayout是谷歌推出替代PrecentLayout的组件。支持相对布局、线性布局、帧布局,笔者看来更像是FrameLayout 、LinearLayout、RelativeLayout三者的结合体,并且比这三者更强大的是实现了百分比布局,大家都知道安卓碎片严重,使用百分比适配,那么将彻底解决适配问题。
本文将教会你如何使用此控件。
一、当作Relative...
ConstraintLayout的尺寸 dimensions有时候,我们需要创建一些固定方向比的 View 组件,最常使用固定横纵比的就是当 ImageView 用于展示一些固定横纵比的图片的时候。举些例子,书面封面(尺寸横纵比多种多样),电影海报(一般是 4:6 ),电影剧照(一般是 1.85:1 或 2.39:1 ),电视剧(一般是 4:3 或 16:9 )对于不熟悉什么是横纵比的,横纵比就是...
<?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"
andro.