问题
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?
回答1:
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:
Therefore, this must be something about your layout. Make sure that in your redefined constructors, your call the super constructors first.
回答2:
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.
来源:https://stackoverflow.com/questions/8184961/relativelayout-doesnt-draw-children-if-background-is-set