0

我有两个按钮。我希望一个在应用程序的顶部中心对齐。我想让另一个按钮忽略重力设置并去我告诉它的地方。我该怎么做呢?

<?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>
4

1 回答 1

2

不要使用RelativeLayout 的gravity 属性,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 教程RelativeLayout.LayoutParams 文档

于 2010-09-20T04:55:42.747 回答