Android - fill_parent 在relativelayout 中的行为不符合预期

标签 android layout button center android-relativelayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_gradient" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/buttonLog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/log"
            android:onClick="log" />

    </RelativeLayout>

</FrameLayout>

我希望我的按钮出现在屏幕中央。但是,它出现在屏幕的顶部中心(即按钮水平居中,而不是垂直居中)。

在我看来,RelativeLayout 的行为就像是用“wrap_content”而不是“fill_parent”定义的。

有趣的是,如果我给我的RelativeLayout高度属性(android:layout_height)一个实际值,比如:

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:gravity="center" >

然后按钮行为正确(即按钮也垂直居中)。但我不想使用实际值。我想使用 fill_parent!为什么它不能与“fill_parent”一起使用??

有谁知道这是怎么回事吗?

提前谢谢您!

最佳答案

RelativeLayout 要求您指定元素在布局中的位置。我没有看到任何layout_below 或layout_toLeftOf 标签。重力适用于 LinearLayouts。一般来说,LinearLayouts 更容易使用,并且它们可以更好地适应不同的屏幕尺寸。我建议您将RelativeLayout 替换为LinearLayout,并将FrameLayout 替换为LinearLayout。如果您想使用多个重叠布局,则通常会使用 FrameLayout,但您不会这么做。

我建议您阅读 Android sdk 引用文档中有关使用布局的内容,如下所示:http://bit.ly/djmnn7

关于Android - fill_parent 在relativelayout 中的行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10843893/

相关文章:

Android Lollipop EditText : AutoComplete Not Working For Email Addresses

php - php中 undefined index 图像

java - Android 应用程序在启动新 Intent 时崩溃

html - 如何在调整屏幕大小时将关闭按钮放置到弹出 div 的顶部而不让它四处移动

javascript - 如何在禁用时更改按钮的颜色然后在启用时返回

android - 为什么我的程序崩溃?

android - 使用 Android 云将图像发送到设备消息传递框架

html - 不使用 CSS3 的边缘更具吸引力和美观

Android:在同一 Activity 中更改布局

flutter - 如何在 Flutter 中的行小部件内的容器上添加边框?