Ich verwende ein lineares Layout, um einen ziemlich hellen Anfangsbildschirm anzuzeigen. Es hat eine Taste, die horizontal und vertikal auf dem Bildschirm zentriert werden soll. Egal, was ich versuche, der Button wird die Mitte nach oben ausrichten. Ich habe das XML unten eingefügt. Kann mir jemand die richtige Richtung weisen?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent">
<ImageButton Android:id="@+id/btnFindMe"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center_vertical|center_horizontal"
Android:background="@drawable/findme"></ImageButton>
</LinearLayout>
Wenn Sie ein Element in der Mitte des Bildschirms zentrieren möchten, verwenden Sie keine LinearLayout
, da diese zur Anzeige einer Anzahl von Elementen in einer Reihe gedacht sind.
Verwenden Sie stattdessen eine RelativeLayout
.
So ersetzen Sie:
Android:layout_gravity="center_vertical|center_horizontal"
für die relevante RelativeLayout
-Option:
Android:layout_centerInParent="true"
So sieht Ihre Layoutdatei so aus:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout Android:id="@+id/RelativeLayout01"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
xmlns:Android="http://schemas.Android.com/apk/res/Android">
<ImageButton Android:id="@+id/btnFindMe"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:background="@drawable/findme"></ImageButton>
</RelativeLayout>
Mit einem LinearLayout zentrieren:
<LinearLayout
Android:id="@+id/LinearLayout1"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:gravity="center"
Android:orientation="vertical" >
<ImageButton
Android:id="@+id/btnFindMe"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:background="@drawable/findme" />
</LinearLayout>
Haben Sie versucht, Android:gravity="center_vertical|center_horizontal"
innerhalb des Layouts zu definieren und Android:layout_weight="1"
im Bild festzulegen?
Eine häufig verwendete Methode, die mit linearem Layout funktioniert, besteht darin, eine Eigenschaft für die Bildschaltfläche festzulegen
Android:layout_gravity="center"
Sie können wählen, ob Sie jedes Objekt im linearen Layout links-, zentriert oder rechtsbündig ausrichten möchten. Beachten Sie, dass die obige Zeile genau dieselbe ist wie
Android:layout_gravity="center_vertical|center_horizontal"
Füge das hinzu
Android:gravity="center"
In LinearLayout.
einfach damit
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:layout_weight="1"
Android:visibility="visible"
Android:gravity="center"
Android:orientation="vertical" >
<ProgressBar
Android:id="@+id/pbEndTrip"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
/>
<TextView
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_marginTop="10dp"
Android:gravity="center"
Android:text="Gettings" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent">
<ImageButton Android:id="@+id/btnFindMe"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity = "center"
Android:background="@drawable/findme">
</ImageButton>
</LinearLayout>
Der obige Code wird funktionieren.
Sie können RelativeLayout
verwenden.
Wie in der Android-Dokumentation für XML-Attribute von Android: layout_gravity ist dies problemlos möglich :)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<ImageButton Android:id="@+id/btnFindMe"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:background="@drawable/findme"></ImageButton>
</LinearLayout>
Wenn Sie LinearLayout
verwenden, können Sie den Schwerpunkt hinzufügen:
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="vertical"
Android:gravity="center">
<Button
Android:layout_width="200dp"
Android:layout_height="wrap_content"
/>
</LinearLayout>`
komplette und Arbeitsprobe aus meiner Maschine ...
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:orientation="vertical"
tools:context=".MainActivity"
Android:gravity="center"
Android:textAlignment="center">
<TextView
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:textAppearance="?android:attr/textAppearanceLarge"
Android:text="My Apps!"
Android:id="@+id/textView"
Android:gravity="center"
Android:layout_marginBottom="20dp"
/>
<Button
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:text="SPOTIFY STREAMER"
Android:id="@+id/button_spotify"
Android:gravity="center"
Android:layout_below="@+id/textView"
Android:padding="20dp"
/>
<Button
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:text="SCORES"
Android:id="@+id/button_scores"
Android:gravity="center"
Android:layout_below="@+id/textView"
Android:padding="20dp"
/>
<Button
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:text="LIBRARY APP"
Android:id="@+id/button_library"
Android:gravity="center"
Android:layout_below="@+id/textView"
Android:padding="20dp"
/>
<Button
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:text="BUILD IT BIGGER"
Android:id="@+id/button_buildit"
Android:gravity="center"
Android:layout_below="@+id/textView"
Android:padding="20dp"
/>
<Button
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:text="BACON READER"
Android:id="@+id/button_bacon"
Android:gravity="center"
Android:layout_below="@+id/textView"
Android:padding="20dp"
/>
<Button
Android:layout_width="220dp"
Android:layout_height="wrap_content"
Android:layout_centerInParent="true"
Android:text="CAPSTONE: MY OWN APP"
Android:id="@+id/button_capstone"
Android:gravity="center"
Android:layout_below="@+id/textView"
Android:padding="20dp"
/>
</LinearLayout>
Sie können auch die Breite des LinearLayout auf wrap_content
setzen und Android:layout_centerInParent
, Android:layout_centerVertical="true"
verwenden:
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:orientation="vertical"
Android:layout_width="wrap_content"
Android:layout_height="fill_parent"
Android:layout_centerInParent="true"
Android:layout_centerHorizontal="true"
Android:layout_centerVertical="true"
>
<ImageButton Android:id="@+id/btnFindMe"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:background="@drawable/findme"/>
benutzen Sie einfach (um es in die Mitte Ihres Layouts zu bringen)
Android:layout_gravity="center"
und verwenden
Android:layout_marginBottom="80dp"
Android:layout_marginTop="80dp"
position ändern
Sie können diesen Code auch ausprobieren:
<LinearLayout
Android:id="@+id/linear_layout"
Android:layout_width="fill_parent"
Android:layout_height="0dp"
Android:layout_gravity="center"
Android:orientation="horizontal"
Android:weightSum="2.0"
Android:background="@drawable/anyBackground" >
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:orientation="vertical"
Android:layout_weight="1" >
<ImageView
Android:id="@+id/img_mail"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:background="@drawable/yourImage" />
</LinearLayout>
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:layout_gravity="center"
Android:orientation="vertical"
Android:layout_weight="1" >
<ImageView
Android:id="@+id/img_save"
Android:layout_width="wrap_content"
Android:layout_height="match_parent"
Android:layout_gravity="center"
Android:background="@drawable/yourImage" />
</LinearLayout>
</LinearLayout>
Setzen Sie im Button den Wert Android:layout_alignParentTop="true"
und Android:layout_centerHorizontal="true"
wie folgt:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
>
<Button
Android:id="@+id/switch_flashlight"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="@string/turn_on_flashlight"
Android:textColor="@Android:color/black"
Android:onClick="action_trn"
Android:background="@Android:color/holo_green_light"
Android:layout_alignParentTop="true"
Android:layout_centerHorizontal="true"
Android:padding="5dp" />
</RelativeLayout>