13

aşağıdaki düzen dosyasını inceleyin: layout_constraintDimensionRatio çalışır:ConstraintLayout boy oranı

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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.support.constraint.ConstraintLayout 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FF0000" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <ImageView 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:background="#0000FF" 
      android:padding="16dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintTop_toTopOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintDimensionRatio="H,3:1" 
      tools:layout_editor_absoluteX="16dp" /> 

    </android.support.constraint.ConstraintLayout> 

</RelativeLayout> 

Ben app nasıl emin değilim. Benim anlayışım her zaman genişlik olacak: yükseklik. Dolayısıyla 3: 1, ImageView'ı daima 3 kattan daha geniş gösterecektir. H veya W öneki ConstraintLayout'a hangi boyutun orana uyması gerektiğini söyler. Eğer H ise o zaman genişlik ilk önce diğer kısıtlardan hesaplanacak ve yükseklik en boy oranına göre ayarlanacaktır.

enter image description here

yüksekliği beklenmedik bir genişliğinden 3 kat daha büyüktür: Bununla birlikte, bu düzen bir sonucudur. Birinin boyutların uygulamaya göre nasıl hesaplandığını açıklayabilir miyim: layout_constraintDimensionRatio ayarı?

cevap

5

bu ImageView özelliklerine bir göz atın:

app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 

Bu özellikler layout_constraintDimensionRatio nedeniyle hangi ImageView alt, üstüne kısıtlanır ve sol işgal View sonuçlanan ana ebeveynin sol geçersiz, Ana ekranın üst ve alt kısımları.

app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 

Bu bir çözüm olabilir. Görünüm üst veya tersi görünmesini istiyorsanız layout_constraintBottom_toBottomOf atlayabilirsiniz. Muhtemelen en iyi en çözüm önerdi olacağını layout_constraintDimensionRatio hariç tamamen her şeyden kısıtlamaları kaldırmak olacaktır. Bu arada app:layout_constraintDimensionRatio işler için

20

Sizin anlayış doğrudur. Eğer app:layout_constraintDimensionRatio="H,3:1" ayarlanırsa en-boy oranına göre genişliği ilk diğer kısıtlamaları hesaplanır ve daha sonra yüksekliği ayarlanabilir anlamına gelir. uygulamanıza tek sorun app:layout_constraintDimensionRatio gözardı edilmesine neden böylece, ImageView app:layout_constraintBottom_toBottomOf="parent" eklendi olmasıdır. 1 en boy oranına: Burada

3 daki ImageView boyutuna düzen var

<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" 
    android:background="#FF0000"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="16dp" 
     android:layout_marginEnd="16dp" 
     android:background="#0000FF" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintDimensionRatio="H,3:1" /> 

</android.support.constraint.ConstraintLayout> 

ve burada sonuç görünümü var:

ImageView in 3:1 aspect ratio

İlgili konular