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