Не удается заставить IgnoreGravity в RelativeLayout работать - PullRequest
1 голос
/ 20 сентября 2010

У меня есть две кнопки. Я хочу, чтобы один был выровнен по верхнему центру приложения. Я хочу получить другую кнопку, чтобы игнорировать настройки гравитации и идти туда, куда я говорю. Как мне это сделать?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="fill_parent" 
    android:gravity="center_horizontal"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/the_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered Button"
        android:layout_alignTop="@id/relative_layout" />

    <Button
        android:id="@+id/the_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Random Button"
        android:ignoreGravity="@id/relative_layout" />

 </RelativeLayout>

1 Ответ

2 голосов
/ 20 сентября 2010

Не используйте гравитационный атрибут RelativeLayout, элементы в RelativeLayout могут быть размещены с дополнительными атрибутами. Попробуйте это:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" >
    <Button
    android:id="@+id/the_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Centered Button"
    android:layout_centerHorizontal="true" />
    <Button
    android:id="@+id/the_button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Random Button"/>
</RelativeLayout>

См. Учебник RelativeLayout и Документы RelativeLayout.LayoutParams для получения дополнительной информации об использовании RelativeLayout.

...