с помощью Android: layout_gravity - PullRequest
0 голосов
/ 10 сентября 2011

Я новичок в разработке Android. Я хочу знать, как использовать атрибут android: layout_gravity и могу ли я использовать эту опцию, чтобы расположить представление в центре ширины экрана .??

Я попробовал следующее, но оно выравнивается сверху

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

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Table Layout" />

    <TableRow>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Username"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:hint="username" />
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Password"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:hint="password"
            android:inputType="textPassword" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="SAVE" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="CANCLE" />
    </TableRow>

</TableLayout>

и мой вывод:

output

Ответы [ 2 ]

5 голосов
/ 10 сентября 2011

В Android есть

android:gravity и android:layout_gravity

android: gravity - Используется для перемещения содержимого вида или макетадля которого установлено это свойство. android: layout_gravity - Используется для перемещения самого вида или макета в любую позицию.

Вот хорошая демонстрацияи объяснение того, как это работает.

Гравитация и layout_gravity

0 голосов
/ 17 апреля 2015
<Button
   android:id="@+id/button2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="0.5"
   android:text="CANCEL" 
   android:gravity="LEFT"
   android:orientation="horizontal"
/>
...