Die Anleitung zu Android FrameLayout
View more Tutorials:
FrameLayout ist ein einfaches Layout. Es kann eine oder mehrere untergeordnete View enthalten, die sich überlappen können. Daher wird das Attribut android:layout_gravity verwenden um die untergeordneten View zu positionieren.

Insbesondere hat FrameLayout 9 Schwerkraftbereiche, die in der folgenden Abbildung dargestellt sind. Achtung: Es handelt sich um die imaginäre Bereiche. Das bedeutet nicht, dass die Oberfläche von FrameLayout in 9 Teilen unterteilt wird.

Wenn ein View in dem FrameLayout hinzugefügt wird, liegt diese standardmäßig in der Schwerkraft von "left|top". Schauen Sie sich das folgende Beispiel an. Ich füge dem FrameLayout 2 Button hinzu. Standardmäßig liegen sie in der Schwerkraft von "left|top" und Sie sehen, dass sie sich überlappen.

Verwenden Sie das Attribut android:layout_gravity um die Position von button anzupassen.

Der Wert vom android:layout_gravity ist die Verbindung eines der folgenden Werten:

Constant in Java | Value | Description |
Gravity.LEFT | left | |
Gravity.CENTER_HORIZONTAL | center_horizontal | |
Gravity.RIGHT | right | |
Gravity.TOP | top | |
Gravity.CENTER_VERTICAL | center_vertical | |
Gravity.BOTTOM | bottom | |
Gravity.START | start | |
Gravity.END | end | |
Gravity.CENTER | center | |
Unten ist das die Abbildung, dafür ein VideoView und ein MediaController in einem FrameLayout gestellt werden. Dies spart Anwendungsraum und bietet dem Benutzer eine bessere Benutzererfahrung.

In diesem Beispiel werde ich ein ImageView und 2 TextView in einem FrameLayout stellen, danach stellen Sie ihre Position durch das Attribut android:layout_gravity ein.
Schauen Sie das Beispiel vor:

Show in Portraint screen

Show in Landscape screen
OK. Im Android Studio erstellen Sie ein neues Projekt:
- File > New > New Project > Empty Activity
- Name: FrameLayoutExample
- Package name: org.o7planning.framelayoutexample
- Language: Java
Bereiten Sie eine Datei-Foto vor:

halong.png
Kopieren Sie die Datei Foto in dem Verzeichnis "drawable" vom Projekt :

Entwerfen Sie die Interface der Anwendung:

Legen Sie die Einschränkungen (constraint) für FrameLayout fest:

Legen Sie die wichtigen Attribute für ImageView fest um sicherzustellen, dass es FrameLayout erfüllt.
* imageView *
<ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" app:srcCompat="@drawable/halong" />

Und legen Sie das Attribut android:layout_gravity für TextView fest:

Legen Sie text, textColor für TextView fest:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".MainActivity"> <FrameLayout android:id="@+id/frameLayout" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" app:srcCompat="@drawable/halong" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:text="Halong Bay, Vietnam" android:textColor="#FFFFFF" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:text="Photo by intrepidtravel.com" android:textColor="#FFFFFF" /> </FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package org.o7planning.framelayoutexample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }