3

私は現在、画像ビューを含むAndroidウィジェットを開発しています。ウィジェットはサイズ変更可能であると想定されています。

画像はウィジェットの幅全体(これは簡単な部分です)を満たし、ウィジェットの高さの30%を満たし、最大高さ80dipを尊重する必要があります(これは私にはわかりません)。

次のレイアウトでは、画像は高さではなく幅に適切に拡大縮小されます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

        <ImageView
            android:id="@+id/promoShelf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/shelf"
            android:scaleType="fitXY"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

</RelativeLayout>

これを実現するために、ウェイト付きのLinearLayoutを使用しようとしました。これは、次のスニペットで確認できます。android:maxHeightただし、ウェイトを使用すると、尊重されないように見えます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical"
        android:weight_sum="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:maxHeight="80dip"
            android:src="@drawable/my_image"
            android:scaleType="fitXY"
            android:layout_weight="0.3" />

    </LinearLayout>
</RelativeLayout>

したがって、これは(不要な)結果です。

なぜmaxHeight機能していないのですか、または誰かがこれを回避する方法を知っていますか?

4

3 に答える 3

3

標準コンポーネントのみを使用する優れたソリューションがあるかどうかはわかりません。ImageViewただし、カスタムウィジェットを使用すると簡単に実行できます。

public class MyImageView extends ImageView {
    private float weight = 0.3f;
    private int maxHeight;

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (attrs != null) {
            final int[] ids = {android.R.attr.maxHeight};
            final TypedArray array = context.obtainStyledAttributes(attrs, ids);

            if (array != null) {
                maxHeight = array.getDimensionPixelSize(0, 0);
                array.recycle();
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxHeight > 0) {
            final int height = MeasureSpec.getSize(heightMeasureSpec);
            final int result = Math.min(Math.round(height * weight), maxHeight);

            heightMeasureSpec = MeasureSpec.makeMeasureSpec(result, MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

そして、あなたのレイアウトで:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0f0">

    <com.example.MyImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:background="#f00"
        android:maxHeight="80dip"/>

</RelativeLayout>
于 2013-02-27T18:09:47.123 に答える
2

これがうまくいくかどうかはわかりませんが、これを試してください:

ImageViewを容器に包みます。コンテナーをLinearLayoutの高さの 30% にします。はコンテナの に接続ImageViewされ、の最大高さは:bottomImageView80dp

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.7" />

        <FrameLayout 
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3" >
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:maxHeight="80dip"
                android:src="@drawable/my_image"
                android:scaleType="fitXY"
               />
        </FrameLayout>

    </LinearLayout>
</RelativeLayout>
于 2013-02-27T17:50:50.000 に答える
0

ちょっと私はそれが本当に良い解決策ではないことを知っていますが、これは今のところ私の頭にある唯一のものです:P

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom"
    android:orientation="vertical"
    android:weight_sum="1">
 <View android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="0.7"
android:background="#000000"/>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:maxHeight="80dip"
        android:src="@drawable/ic_action_search"
        android:scaleType="fitXY"
        android:background="#800000"
        android:layout_weight="0.3" />

  </LinearLayout>
 </RelativeLayout>
于 2013-02-27T17:32:22.953 に答える