2017-08-22 2 views
0

Ich versuche, eine Schaltfläche am oberen Rand des Bildschirms und darunter ein ImageView, die den Bildschirm (alle Bildschirmgrößen) füllen müssen. In RelativeLayout scheint es einfach zu sein, aber im ConstraintLayout gelingt mir das nicht.Wie füllt man den Bildschirm mit zwei View in ConstraintLayout?

Here is an example of what it's look like in RelativeLayout:

und die XML-Code:

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Download Image" 
    android:id="@+id/button" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:onClick="downloadImage" /> 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/imageView" 
    android:layout_below="@+id/button" 
    android:layout_alignParentStart="true" /> 

Danke für Ihre Hilfe, Shay.

+0

Möchten Sie Bild als Hintergrund und Schaltfläche im Vordergrund? –

Antwort

0
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Download Image" 
    android:id="@+id/button" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" 
    android:onClick="downloadImage" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent"/> 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/imageView" 
    android:layout_below="@+id/button" 
    android:layout_alignParentStart="true" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/button" 
    app:layout_constraintBottom_toBottomOf="parent"/> 

Schaltfläche muss links, rechts und oben Einschränkungen mit übergeordneten haben.

Und ImageView muss links, rechts, unten Einschränkungen mit übergeordneten und oberen Einschränkung mit Knopf unten haben.

+0

Vielen Dank Herr. sehr hilfreicher Kommentar. –

1

Vielleicht Einvernehmen darüber Ich habe Ihre Frage nicht ganz, aber das ist, was ich tat, um das Bild neu erstellen Sie als Beispiel gab:

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

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:text="Button" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <ImageView 
     android:id="@+id/imageView27" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintHorizontal_bias="0.59" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/button2" 
     app:srcCompat="@drawable/common_google_signin_btn_icon_dark" /> 
</android.support.constraint.ConstraintLayout> 

und das Ergebnis: enter image description here

0

Eigentlich fill_parent Eigenschaft ist veraltet. verwenden, so 0DP folgenden Code verfügbaren Speicherplatz der ConstraintLayout

Für Ihre Frage Gebrauch zu füllen Button oben des Bildschirms zu zeigen, und darunter ein Image der füllen wird auf jeden Fall den Bildschirm (für alle Bildschirmgrößen).

<?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"> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:onClick="downloadImage" 
     android:text="Download Image" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/button" /> 
</android.support.constraint.ConstraintLayout> 

Hinweis: Wenn Sie Marge/padding hinzufügen möchten dann auf das Layout anwenden oder einer beliebigen Ansicht wie pro Ihre Anforderung. Wenn Sie der Ansicht innerhalb der ConstraintLayout jedoch einen Rand hinzufügen, muss diese Ansicht für diese Partikelseite eine Einschränkung aufweisen.

Verwandte Themen