如果设置了背景,RelativeLayout 不会绘制子元素
我有从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了您的代码,但用relativelayout替换了您的自定义布局:
并且xml:
它工作正常:
因此,这必须与你的布局有关。
确保在重新定义的构造函数中,首先调用超级构造函数。
I tried your code but replaced your custom layout with a RelativeLayout:
And the xml:
It works fine:
Therefore, this must be something about your layout.
Make sure that in your redefined constructors, your call the super constructors first.
它看起来像是 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.