4

画像ボタンがあります。必要に応じて、これをカスタム ビューに置き換えることができます。Image Button の高さを Fill_parent として指定する必要があります。画像が画面内の利用可能な高さまで伸びるようにします。これを行うとき、幅を緩めたくありません。高さとしては最小にしてほしいし、高さ以上なら問題ない。こんなに長くいたくない。これを行う間、アスペクト比も維持したいと思います。この種のビューを取得するために調整する必要があるパラメーターを教えてください。この点に関するあらゆる種類の助けをいただければ幸いです。ご協力いただき、ありがとうございました。

編集:以下は、高さがfill_parentの水平スクロールビューを作成しようとしている完全なxmlで、その高さに収まり、アスペクト比に従って幅を拡大または縮小する画像で埋めようとしています。画像が小さくても横スクロールビューで高さが常に占有されるように拡大したい。

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="3"
        android:background="#666666">
    </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:orientation="vertical" android:layout_weight="7"
        android:background="#888888">
        <HorizontalScrollView android:id="@+id/horizontalScrollView1"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:id="@+id/linearLayout1"
                android:layout_width="fill_parent" android:layout_height="fill_parent"
                android:orientation="horizontal">

                <ImageView android:src="@drawable/image1"
                    android:layout_weight="1" android:layout_width="wrap_content"
                    android:layout_height="fill_parent" android:adjustViewBounds="true"
                    android:id="@+id/attachimagebutton_2" />

                <ImageView android:src="@drawable/image2"
                    android:layout_weight="1" android:layout_width="wrap_content"
                    android:layout_height="fill_parent" android:adjustViewBounds="true"
                    android:id="@+id/attachimagebutton_2" />

            </LinearLayout>
        </HorizontalScrollView>
    </LinearLayout>
</LinearLayout>
4

3 に答える 3

2

まあ、ImageButtonAndroidでは何でもボタンにできるので、それである必要はありません...

ImageViewXML に を追加してid、 、画像ソース、高さfill_parent、幅 をwrap_content指定し、アスペクト比を で修正できますandroid:adjustViewBounds="true"。次にandroid:clickable="true"id. 次に、を追加できますonClickListener

それがうまくいくことを願っています

于 2011-06-21T23:54:51.063 に答える
1

別のアプローチをとることをお勧めします。親レイアウトを適用gravity="center_horizontal"し、次のステップはクラスを拡張しImageButtonてサイズを正方形に設定するカスタム ビューを作成することです。これは非常に簡単です。

新しいクラスを作成し、名前を付けてSquareImageButton、拡張しImageButtonます。コンテキストと属性を使用してデフォルトのコンストラクターを追加し、スーパー関数を呼び出すようにします。他に何も追加する必要はありません。

ここでメソッドをオーバーライドしonMeasure()(測定された幅と高さを int 値で取得します)、最小値を取得し、(メソッドの要求に応じて)setMeasuredDimension(w, h)新しい最小値で呼び出して正方形になるようにします。それは私にとってはうまくいきました。ちょっと迷った場合のコードは次のとおりです。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int minDimension = Math.min(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(minDimension, minDimension);
}
于 2013-06-17T15:38:31.833 に答える