【问题标题】:RelativeLayout doesn't draw children if background is set如果设置了背景,RelativeLayout 不会绘制子级
【发布时间】:2011-11-18 15:43:31
【问题描述】:

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

我也有一个 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 替换它,一切正常。 Android 中的错误?


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

【问题讨论】:

    标签: android background android-relativelayout imagebutton xml-layout


    【解决方案1】:

    我尝试了您的代码,但用 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>
    

    效果很好:

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

    【讨论】:

    • 我尝试用RelativeLayout 替换自定义类。同样的问题。
    • 我发现我遇到了这个问题,如果使用 NinePatch 作为背景。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      相关资源
      最近更新 更多