实际应用
在上一篇文章中只是初步识别了一下效果;今天就来做一个实际应用,拿公司的页面来做效果.
手机上的效果图
Pad 上的效果图
看图我们知道,控件按一定比例放大了,但不能变形,所以上下会留有空白。
先看下我们实现后的效果
1.phone 1920*1080
2.phone 1280*728
3.pad 2948*1536
是不是完美适配了!!!
再来看下如果不用我们的适配布局的效果
好了 看下我们的layout
这是使用系统的ConstraintLayout
<?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"
android:background="#2A3D5D"
tools:context="com.weihu.autolayout.musicdemo.MusicDemoActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="54dp"
android:scaleType="fitXY"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/nav_music"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_4"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageView6"
android:layout_marginLeft="28dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
android:layout_marginStart="28dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_3"
app:layout_constraintRight_toLeftOf="@+id/imageView3"
app:layout_constraintLeft_toRightOf="@+id/imageView5"
android:layout_marginLeft="28dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
android:layout_marginStart="28dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_2"
app:layout_constraintRight_toLeftOf="@+id/imageView6"
app:layout_constraintLeft_toRightOf="@+id/imageView4"
android:layout_marginLeft="28dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
android:layout_marginStart="28dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_1"
app:layout_constraintRight_toLeftOf="@+id/imageView5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView7"
android:layout_width="600dp"
android:layout_height="202dp"
android:layout_marginTop="9dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:srcCompat="@drawable/music_piano"/>
</android.support.constraint.ConstraintLayout>
这是使用AdapterConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<com.weihu.autolayout.AdapterConstraintLayout
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"
android:background="#2A3D5D"
tools:context="com.weihu.autolayout.musicdemo.MusicDemoActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="54dp"
android:scaleType="fitXY"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/nav_music"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_4"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/imageView6"
android:layout_marginLeft="28dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
android:layout_marginStart="28dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_3"
app:layout_constraintRight_toLeftOf="@+id/imageView3"
app:layout_constraintLeft_toRightOf="@+id/imageView5"
android:layout_marginLeft="28dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
android:layout_marginStart="28dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_2"
app:layout_constraintRight_toLeftOf="@+id/imageView6"
app:layout_constraintLeft_toRightOf="@+id/imageView4"
android:layout_marginLeft="28dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
android:layout_marginStart="28dp"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="84dp"
android:layout_height="84dp"
app:srcCompat="@drawable/music_1"
app:layout_constraintRight_toLeftOf="@+id/imageView5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
app:layout_constraintTop_toBottomOf="@+id/imageView2"/>
<ImageView
android:id="@+id/imageView7"
android:layout_width="600dp"
android:layout_height="202dp"
android:layout_marginTop="9dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
app:srcCompat="@drawable/music_piano"/>
</com.weihu.autolayout.AdapterConstraintLayout>
单位都是使用dp,因为设计图是1920*1080 所以px转dp就使用px/3。
为什么使用dp不直接使用px呢?因为ConstraintLayout 在拖动控件时会把px转为dp,要是我们用px设计好布局后一动就都转为px是不是想死的心都有!!所以干脆直接使用dp
好了
代码传送门