2017-11-08 10 views
6

mam do czynienia z pozornie prostym zadaniem ma układ z:ConstraintLayout: miejsce TextView poniżej ImageView

  • An ImageView biorąc pełną szerokość, wysokość zgodnie z proporcjami obrazka
  • TextView niższym niż

to co mam

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/hamster" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText " 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/image_view"/> 

</android.support.constraint.ConstraintLayout> 

I to właśnie wygląda:

enter image description here

Jak widać, ImageView ma margines do góry, które nie chcę.

Gdybym zmienić ImageView's ograniczeń z

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

do

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

(usuwanie dolne ograniczenie), wynik wygląda tak:

enter image description here

Teraz ImageView jest o godz e bardzo na szczycie mojego układu, ale odszedł TextView. Przeszkadza Ci, dla drugiego wariantu, podgląd układ Android Studio 3 pokazuje układ dokładnie jak bym go chciał:

enter image description here

Co robię źle i jak mogę to naprawić?

Uwaga: Układ musi być w formacie ConstraintLayout, a wysokość układu musi wynosić wrap_content.

Możesz znaleźć mój projekt testowy here.

EDYCJA: Problem wydaje się występować tylko na większych urządzeniach. Używam emulatora Nexus 9.

+1

Próbowałaś użyciem 'aplikacji: layout_constraintVertical_bias = 0.0' –

+0

@Ascorbin, praca nirva szacha roztworu użytkowego ?? –

+0

@ShwetaChauhan Nope, zobacz mój komentarz poniżej jego odpowiedzi. – FWeigl

Odpowiedz

2

Tak jak Akash Amin powiedział dodaj dwie linie w ImageView

app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintVertical_bias="0.0" 
+0

Dodanie tego do ImageView rozwiązało problem, dzięki. – FWeigl

+0

cieszę się, że mogę ci pomóc. –

0

Spróbuj to było pomocne dla Ciebie


<?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"> 


    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:srcCompat="@color/colorAccent" 
     app:layout_constraintBottom_toTopOf="@+id/textView2"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="0dp" 
     android:text="MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText " 
     android:textAlignment="center" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/imageView2" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" /> 
</android.support.constraint.ConstraintLayout> 

enter image description here

+0

Wynikiem tego jest http://i66.tinypic.com/24nps1e.jpg, więc niestety nie to, co chciałem. – FWeigl

0

Try to ..

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@mipmap/ic_launcher" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText " 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/image_view" /> 

</android.support.constraint.ConstraintLayout> 

Delete bootom contraint z ImageView i ustawić TextView górną ograniczenie do dołu ImageView

+0

Niestety, problem nadal występuje z twoim xml na większych urządzeniach. Zmieniłem moje pytanie. – FWeigl

0
<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="0dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/ic_launcher_background" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="@id/guideline" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="MyText MyText MyText MyText MyText MyText MyText 
     MyText MyText MyText MyText MyText MyText MyText MyText MyText 
     MyText 
     MyText MText MyText MyText MyText MyText MyText MyText MyText 
     MyText 
     MyText MyText MyText MyText MyText MyText MText MyText MyText 
     MyText 
     MyText MyText MyText MyText MyText MyText MyText MyText MyText t 
     MyText MyText MyText MyText " 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/guideline" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/guideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.45" /> 

</android.support.constraint.ConstraintLayout> 
+0

Powinieneś użyć Ctrl + K lub Cmd + K, aby poprawnie formatować kawałki kodu źródłowego. –

Powiązane problemy