Ist es möglich, in Android einen Blick auf einem anderen zu platzieren?
-
26-09-2019 - |
Frage
Können wir einen kleinen Blick auf eine andere große Ansicht platzieren? Zum Beispiel habe ich eine Videoview, die eine Datei im Hintergrund spielt. Über diese, irgendwo in der Mitte / Ecke, möchte ich noch einen Image platzieren.
Aber in Linear / Relative-Layout können Ansichten nur eine nach ihnen einem anderen oder relativ platziert werden, und AbsoluteLayout wird abgeraten. Also, was soll ich tun?
Lösung
FrameLayouts
können Sie jede Ansicht auf dem ein Stapel unten. Dies kann auch mit einem RelativeLayout
erreicht werden.
Andere Tipps
Die FrameLayout
ist die einfachste ViewGroup
und stapelt die Views
in der Reihenfolge, wie sie in Layout XML definiert sind (oder hinzugefügt programmatisch); die zunächst niedriger, und die letzten auf der Oberseite sein wird.
Hier ist ein Beispiel, wo zwei Views
gestapelt und Offset Zum besseren Veranschaulichung der Punkt:
Dies ist das tatsächliche Layout XML mit den zwei überlappenden TextView
Box. Der Versatz der beiden Boxen erfolgt mit android:layout_gravity
während android:gravity
innerhalb jeder Box zum Zentrieren des Textes selbst verwendet wird.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp">
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="top|left"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:text="First is below"/>
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|right"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:text=" Last is on top"/>
</FrameLayout>
Nur für den Fall, wenn Sie einen Blick auf einen ButtonView platzieren mögen dann diese verwenden;
android:elevation="7dp"
für die Ansicht, die oben auf den Button gelegt werden muss.
Sie können es auch tun, indem Sie ConstraintLayout ein neues Layout eingeführt von google.
ConstraintLayout können Sie große und komplexe Layouts mit einer flachen Ansicht Hierarchie erstellen (keine verschachtelten Ansicht Gruppen).
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="4"
tools:context="com.edalat.example.MainActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="283dp"
android:layout_height="349dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="24dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="24dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.509"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="24dp"
android:layout_marginLeft="24dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="24dp"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/dp_20"
android:background="@color/yellow"
>
<VideoView
android:id="@+id/videoview"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="300dp"
android:contentDescription="@string/app_name"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/camera"
android:layout_margin="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:id="@+id/imageView" />
</RelativeLayout>