android studio 3.1.2 布局文件

吾生也有涯,而知也无涯。这篇文章主要讲述android studio 3.1.2 布局文件相关的知识,希望能为你提供帮助。
1.   新进引入了ConstraintLayout ,在AS 2.3之后的新建Module默认布局就是 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" tools:context="com.constraintlayout.app.Main2Activity"> < /android.support.constraint.ConstraintLayout>

 
在使用  ConstraintLayout  的布局方案,需要在  build.gradle  引入支持库:
implementation ‘com.android.support.constraint:constraint-layout:1.1.0‘

传统的Android开发当中,界面基本都是靠编写XML代码完成的,虽然Android Studio也支持可视化的方式来编写界面,但是操作起来并不方便,我也一直都不推荐使用可视化的方式来编写Android应用程序的界面。
而ConstraintLayout就是为了解决这一现状而出现的。它和传统编写界面的方式恰恰相反,ConstraintLayout非常适合使用可视化的方式来编写界面,但并不太适合使用XML的方式来进行编写。当然,可视化操作的背后仍然还是使用的XML代码来实现的,只不过这些代码是由Android Studio根据我们的操作自动生成的。
另外,ConstraintLayout 还有一个优点,它可以有效地解决布局嵌套过多的问题。我们平时编写界面,复杂的布局总会伴随着多层的嵌套,而嵌套越多,程序的性能也就越差。ConstraintLayout则是使用约束的方式来指定各个控件的位置和关系的,它有点类似于 RelativeLayout,但远比RelativeLayout要更强大
constraintDimensionRatio【android studio 3.1.2 布局文件】这个属性就是把一个View的尺寸设为特定的宽高比,比如设置一张图片的宽高比为 1:1,4:3, 16:9 等。通过使用ConstraintLayout,只需使用layout_constraintDimensionRatio属性即可。
< ?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:id="@+id/constraintLayout" tools:context="com.constraintlayout.app.MainActivity" > < ImageView android:id="@+id/cat_image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintDimensionRatio="4:3" app:layout_constraintRight_toRightOf="parent" app:layout_constraintHorizontal_bias="0" android:src="https://www.songbingjia.com/android/@mipmap/cat" /> < ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:src="https://www.songbingjia.com/android/@mipmap/ic_launcher" app:layout_constraintDimensionRatio="4:3" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/cat_image" app:layout_constraintVertical_bias="0.0" app:layout_constraintRight_toRightOf="parent" /> < /android.support.constraint.ConstraintLayout>

 

    推荐阅读