1

からクラスを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背景が設定されている場合、なぜレンダリングされないのかということです。

4

2 に答える 2

0

コードを試しましたが、カスタムレイアウトを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>

それはうまくいきます:

スクリーンショット

したがって、これはレイアウトに関するものでなければなりません。再定義されたコンストラクターで、最初にスーパーコンストラクターを呼び出すようにしてください。

于 2011-11-18T16:04:15.700 に答える
0

Android2.3.3のバグのようです。PNGファイルをバックグラウンドとしてNinePatchとして使用していますが、この問題が発生します。別のNinePatchを使用すると、すべてが正常に機能します。

この問題をAndroid3.1で再現することはできません。

于 2011-12-01T11:37:39.263 に答える