相对布局与背景图像的对齐怪异

发布于 2024-11-08 19:10:04 字数 1092 浏览 0 评论 0原文

我在RelativeLayout中有一个9 patch png,一切看起来都很棒!但是,当我在 RL 中创建 textView 时,textView 不在父级的顶部...我也在手机上测试了这一点,同样的结果...为什么会这样做?

感谢您的帮助!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testLL"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backrepeat"
    >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="15dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="@drawable/contentbox">

        <TextView
            android:text="Test"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>

    </RelativeLayout>
</LinearLayout>

RelativeView TextView

I have a 9 patch png in a RelativeLayout and everything looks great! However, when i create a textView in the RL, the textView is not at the top of the parent... I also tested this on the phone, same result... why does it do this?

Thanks for your assistance!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testLL"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backrepeat"
    >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="15dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:background="@drawable/contentbox">

        <TextView
            android:text="Test"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"/>

    </RelativeLayout>
</LinearLayout>

RelativeView
TextView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2024-11-15 19:10:04

在查找 9patches 的渲染方式时,它基本上将不可拉伸的区域变成了默认的布局填充。这是为了方便将内容轻松输入到目标(可拉伸)区域,而无需手动定义填充。通过分配 9patch,您正在使用它的填充。它根据 9patch 到达可拉伸中心之前的像素数将填充分配给左上角、右下角和底部。

您可以尝试在 textView 中执行类似 android:paddingTop="-50px" 的操作,看看会发生什么。我还没有测试过这个,所以我有兴趣看看结果如何。

用图片编辑您的评论:由于您的 9patch 的顶部弹性区域不会从大约 90 px 开始,因此它会自动将第一个元素向下填充 90 px,以将其放置在“弹性目标”区域内。尝试我上面的建议,看看它是如何工作的,我很想看看结果如何。我的 IDE 无法从我的手机访问,否则我会为您测试它;)

编辑:我很抱歉,我一直在说“填充”而不是边距。但理论是一样的。不修改上述内容以永久记录我的白痴行为。

In looking up how 9patches are rendered, it basically turns the non-stretchable areas into default layout padding. This is to facilitate easy entry of stuff into the target (stretchable) area, without having to go in and define paddings manually. By assigning a 9patch, you are using it's padding. It assigns paddings to top left right and bottom based on how many px the 9patch has until it reaches the stretchable center.

You may try doing something like android:paddingTop="-50px" in your textView and see what happens. I haven't tested this, so I'd be interested to see how it turns out.

Edit to your comment with pic: Since your 9patch's top stretchy region doesnt start for about 90 px, its automatically going to pad the first element 90 px down, to place it within the "stretchy target" region. Try my above suggestion to see how it works, I'm pretty interested to see how it turns out. I don't have my IDE accessible from my Mobile, otherwise I'd test it for you ;)

Edit: I apologize, I've been saying "padding" instead of margin. But the theory is the same. Not modifying the above for permanent documentation of my idiocy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文