1

画面上の画像ボタンに画像を動的にロードしています。画像は事前に知らされておらず、差分サイズです。

imagebutton のサイズが割り当てられたイメージによって変更されないようにする方法を知っている人はいますか。ボタンに合わせて画像を引き伸ばすことをお勧めします。android:scaleType="fitXY" を使用して、幅と高さをハード値に設定してみました (好ましくありません)。私が何をしても、画像はボタンのサイズを決定し、その逆ではありません! ヘルプ!

以下の組み合わせでやってみました。画像はインターネットの検索からランダムに取得されます。

                    <ImageButton
                        android:id="@+id/ImageButton1"
                        android:layout_height="93px"
                        android:src="@drawable/image1"
                        android:layout_width="93px"
                        android:background="93px" />
                    <ImageButton
                        android:id="@+id/ImageButton4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/image1"
                        android:scaleType="fitXY" />

どんな助けでも大歓迎です!

4

2 に答える 2

5

各 ImageButton のサイズを設定する代わりに、ボタンをコンテナー レイアウトに配置し、コンテナーのサイズを設定します。例えば:

<LinearLayout
        android:id="@+id/container"
        android:layout_width="80dp"
        android:layout_height="80dp" >

        <ImageButton
            android:id="@+id/ImageButton1"
            android:src="@drawable/image1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:scaleType="fitXY" />
</LinearLayout>

これにより、画像ボタンは、それが存在するコンテナーのサイズよりも大きくならないようにする必要があります。

于 2012-10-19T17:39:00.990 に答える
1

あなたが持っているビットマップのサイズは異なります。それらはすべて、アプリケーションに適した newHeight と newWidth に変換できます。そのため、サイズは一定のままです。以下のコードを使用して、適切な幅と高さにします。

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
于 2012-10-19T17:39:43.103 に答える