【问题标题】:Why is layout_marginBottom ignored when using wrap_content?为什么在使用 wrap_content 时会忽略 layout_marginBottom?
【发布时间】:2012-05-10 06:59:51
【问题描述】:

我有以下布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FF0000" >

<RelativeLayout
    android:id="@+id/usericon_img"
    android:layout_width="73.3334dp"
    android:layout_height="70.6667dp"
    android:layout_marginBottom="2.6667dp"
    android:layout_marginLeft="3.3334dp"
    android:layout_marginTop="2.6667dp"
    android:background="#FFFFFF" />
</RelativeLayout>

在模拟器上(我在真机上测试过,看起来一样)布局如下:

我的问题是:为什么我在白色布局下没有红色边距?我知道如果我将外部布局更改为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="76dp"
android:background="#FF0000" >

我会得到我想要的,但为什么wrap_content 不能正常工作?

【问题讨论】:

    标签: android margin android-relativelayout


    【解决方案1】:

    documentation

    要测量其尺寸,视图会考虑其填充。

    如果我没记错的话,这意味着边距没有添加到维度中。这意味着 wrap_content 可以正常工作,但不考虑边距。

    只是一个简短的问题:为什么要使用 70.6667dp 这样的值?

    【讨论】:

    • 我使用了 70.6667,因为我希望 hdpi 屏幕上的尺寸为 106 像素。
    • 如果忽略边距,为什么我有左边距和上边距?
    • 在测量尺寸时会忽略边距,但您仍然可以看到它们(至少顶部和左侧可见,而底部将在可见的红色区域之外)。
    • @Toorop,我来晚了,但你意识到 HDPI 的 DPI 只是近似值吗?
    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多