Макет Android установлен двумя кнопками по центру по вертикали и горизонтали - PullRequest
0 голосов
/ 14 января 2011

Я разрабатываю приложение для Android.

Я пытаюсь установить кнопки сохранения и закрытия по центру, но они отображаются слева.

Вот мой код XML-макета:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialogGameName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="2sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="5sp"
        android:typeface="sans"
        android:textSize="26sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/dialogGameDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="2sp"
        android:typeface="monospace"
        android:textSize="18sp"/>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical|center_horizontal">
        <Button
            android:id="@+id/registerGameButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:gravity="center"
            android:layout_marginBottom="5sp"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="2sp"
            android:layout_marginTop="0sp" />
        <Button
            android:id="@+id/closeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/close" 
            android:gravity="center"
            android:layout_marginBottom="5sp"
            android:layout_marginLeft="2sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="0sp"/>
    </LinearLayout>
</LinearLayout>

Ответы [ 3 ]

5 голосов
/ 14 января 2011

Вы не можете ничего центрировать, если его родитель больше этого. У вас есть родительский элемент для кнопок как wrap_content, поэтому нет места для центрирования ... попробуйте это для кнопок:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center">
    <Button
        android:id="@+id/registerGameButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="2sp"
        android:layout_marginTop="0sp" />
    <Button
        android:id="@+id/closeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" 
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"/>
</LinearLayout>
3 голосов
/ 14 января 2011

Добавить:

android:gravity="center_horizontal"

К вашему горизонтальному линейному макету, который содержит две ваши кнопки.

«android: layout_gravity» решает, как вид должен позиционироваться вparent, тогда как «android: gravity» решает, как должны быть выровнены дочерние представления

1 голос
/ 14 января 2011

Несколько советов для вас (изменение, которое я внес в ваш xml)

  • android:layout_gravity - это макет для его дочернего элемента, который мы должны установить android:gravity

  • , чтобы получить точный центр, мы устанавливаем android:gravity="center"

  • Чтобы установить дочерний элемент в точном центре, родитель должен быть с android:layout_width="fill_parent" и android:layout_height="fill_parent"

  • если этот макет является дочерним по отношению к любому другому макету, он также должен быть с android:layout_width="fill_parent" и android:layout_height="fill_parent"

    <TextView
        android:id="@+id/dialogGameName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="2sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="5sp"
        android:text="asdff"
        android:typeface="sans"
        android:textSize="26sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/dialogGameDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="asdff"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="2sp"
        android:typeface="monospace"
        android:textSize="18sp"/>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:id="@+id/registerGameButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:gravity="center"
            android:layout_marginBottom="5sp"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="2sp"
            android:layout_marginTop="0sp" />
        <Button
            android:id="@+id/closeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/close" 
            android:gravity="center"
            android:layout_marginBottom="5sp"
            android:layout_marginLeft="2sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="0sp"/>
    </LinearLayout>
    

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...