如果设置了背景,RelativeLayout 不会绘制子元素

发布于 2024-12-16 12:29:01 字数 1792 浏览 0 评论 0原文

我有从 RelativeLayout 扩展的 CalloutView 类。目前没有任何方法,它只是重新定义了构造函数。

我还有一个 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<com.example.CalloutView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70dp">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dp"
        android:layout_marginTop="6dp"
        android:background="@drawable/button_disclose"
        android:src="@drawable/disclose" />

</com.example.CalloutView>

我通过以下代码膨胀了 CalloutView 实例的布局:

this.calloutView = (CalloutView) ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.callout_view, null);
this.calloutView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 105));
this.calloutView.measure(MeasureSpec.makeMeasureSpec(400, MeasureSpec.AT_MOST),  MeasureSpec.makeMeasureSpec(105, MeasureSpec.EXACTLY));

它呈现完美。 ImageButton 像它应该的那样出现在它的右侧。

但是,当我向 CalloutView 添加背景时:

<com.example.CalloutView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:background="@drawable/callout">
...
</com.example.CalloutView>

它仅呈现背景,但不出现 ImageButton。


编辑

我使用 NinePatch 图像作为背景。如果我用非 NinePatch 替换它,一切都会正常。安卓系统有bug吗?


主要问题是,如果设置了背景,为什么 ImageButton 没有渲染?

I have class CalloutView extended from RelativeLayout. Currently has no any methods, it just redefines constructors.

I also have an XML layout for it:

<?xml version="1.0" encoding="utf-8"?>
<com.example.CalloutView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70dp">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dp"
        android:layout_marginTop="6dp"
        android:background="@drawable/button_disclose"
        android:src="@drawable/disclose" />

</com.example.CalloutView>

I inflate layout of instance of CalloutView by this code:

this.calloutView = (CalloutView) ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.callout_view, null);
this.calloutView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 105));
this.calloutView.measure(MeasureSpec.makeMeasureSpec(400, MeasureSpec.AT_MOST),  MeasureSpec.makeMeasureSpec(105, MeasureSpec.EXACTLY));

It renders perfectly. ImageButton appears on the right side of it like it should.

But when I add a background to CalloutView:

<com.example.CalloutView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:background="@drawable/callout">
...
</com.example.CalloutView>

It renders only background, but ImageButton doesn't appear.


Edit

I use NinePatch image as a background. If I replace it with non-NinePatch everything works fine. Bug in Android?


The main question is, why it ImageButton isn't rendered, if background is set?

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

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

发布评论

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

评论(2

牵你的手,一向走下去 2024-12-23 12:29:01

我尝试了您的代码,但用relativelayout替换了您的自定义布局:

RelativeLayout calloutView = (RelativeLayout) ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.main, null);
        calloutView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 105));
        calloutView.measure(MeasureSpec.makeMeasureSpec(400, MeasureSpec.AT_MOST),  MeasureSpec.makeMeasureSpec(105, MeasureSpec.EXACTLY));

        setContentView(calloutView);

并且xml:

<?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="70dp"
    android:background="@drawable/ic_launcher">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dp"
        android:layout_marginTop="6dp"
        android:background="#ff0000"
        android:src="@drawable/ic_launcher"
         />

</RelativeLayout>

它工作正常:

screenshot

因此,这必须与你的布局有关。
确保在重新定义的构造函数中,首先调用超级构造函数。

I tried your code but replaced your custom layout with a RelativeLayout:

RelativeLayout calloutView = (RelativeLayout) ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.main, null);
        calloutView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 105));
        calloutView.measure(MeasureSpec.makeMeasureSpec(400, MeasureSpec.AT_MOST),  MeasureSpec.makeMeasureSpec(105, MeasureSpec.EXACTLY));

        setContentView(calloutView);

And the xml:

<?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="70dp"
    android:background="@drawable/ic_launcher">

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dp"
        android:layout_marginTop="6dp"
        android:background="#ff0000"
        android:src="@drawable/ic_launcher"
         />

</RelativeLayout>

It works fine:

screenshot

Therefore, this must be something about your layout.
Make sure that in your redefined constructors, your call the super constructors first.

你的背包 2024-12-23 12:29:01

它看起来像是 Android 2.3.3 中的一个错误。我使用我的 PNG 文件作为 NinePatch 作为背景,我遇到了这个问题。如果我使用另一个 NinePatch,一切都会正常进行。

我无法在 Android 3.1 上重现此问题。

It looks like a bug in Android 2.3.3. I use my PNG file as NinePatch as background, I get this issue. If I use another NinePatch, everything works like it should.

I can't reproduce this issue on Android 3.1.

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