android studio 3.1.2 布局檔案(1)

來源:互聯網
上載者:User

標籤:gradle   可視化   檔案   ati   控制項   dimen   build   代碼   產生   

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

這個屬性就是把一個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="@mipmap/cat"        />    <ImageView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:src="@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>

 

android studio 3.1.2 布局檔案(1)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.