Ich versuche, ein zu zentrieren ImageView
in einem LinearLayout
horizontal und vertikal, aber ich kann es einfach nicht tun. Der Hauptgrund, warum ich keine verwende RelativeLayout
denn dafür brauche ich die layout_weight
(mein Activity
besteht aus vier Spalten, die gleichmäßig aufgeteilt sein sollten und auch auf unterschiedliche Bildschirmbreiten reagieren, wobei jede Spalte eine hat ImageView
zentriert und ungedehnt).
Hier ist mein XML bisher:
<LinearLayout 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"
android:background="#000"
android:baselineAligned="false"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="https://stackoverflow.com/questions/18051472/@drawable/ic_speak" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_readtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_readtext" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_edit" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_config"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_config" />
</LinearLayout>
</LinearLayout>
Naetmul
android:gravity
behandelt die Ausrichtung seiner Kinder,
android:layout_gravity
übernimmt die Ausrichtung von selbst.
Verwenden Sie also eines davon.
<LinearLayout 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"
android:background="#000"
android:baselineAligned="false"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:id="@+id/imageButton_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="https://stackoverflow.com/questions/18051472/@drawable/ic_speak" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:id="@+id/imageButton_readtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image_bg"
android:src="@drawable/ic_readtext" />
</LinearLayout>
...
</LinearLayout>
oder
<LinearLayout 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"
android:background="#000"
android:baselineAligned="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/image_bg"
android:src="https://stackoverflow.com/questions/18051472/@drawable/ic_speak" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageButton_readtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/image_bg"
android:src="@drawable/ic_readtext" />
</LinearLayout>
...
</LinearLayout>
Tarsem Singh
android:layout_gravity
wird für das Layout selbst verwendet
Verwenden android:gravity="center"
für Kinder von Ihnen LinearLayout
Ihr Code sollte also lauten:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" >
ViTUu
Ich habe die hier genannten Lösungen ausprobiert, aber es hat mir nicht geholfen. Ich denke, die Lösung ist layout_width
benutzen müssen wrap_content
als Wert.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" >
pcodex
Hier ist ein Beispielcode. Das hat bei mir funktioniert.
<LinearLayout
android:gravity="center"
>
<TextView
android:layout_gravity="center"
/>
<Button
android:layout_gravity="center"
/>
</LinearLayout>
Sie entwerfen also das lineare Layout so, dass alle seine Inhalte (TextView und Button) in seiner Mitte platziert werden, und dann werden TextView und Button relativ zur Mitte des linearen Layouts platziert.
android:gravity
kann auf einem Layout verwendet werden, um seine untergeordneten Elemente auszurichten.
android:layout_gravity
kann für jede Ansicht verwendet werden, um sich selbst in ihrer übergeordneten Ansicht auszurichten.
HINWEIS: Wenn sich selbst oder Kinder nicht wie erwartet zentrieren, überprüfen Sie, ob Breite/Höhe stimmt match_parent
und zu etwas anderem wechseln
rahulrvp
Wenn Sie nach einer Antwort im Java-Code suchen,
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setGravity(Gravity.CENTER);
// add children
134703 0 0 cookie-check Wie zentriert man den Inhalt in einem linearen Layout? yes
Posten Sie den Screenshot, wie Sie möchten, und den aktuellen Screenshot
– Yogesh Tatwal
5. August 2013 um 6:06 Uhr