2

ポートレートモードを想定すると、画像のサイズが可変になるImageViewがあります(ただし、常に正方形です)。ImageView の幅を常にレイアウトの幅 (fill_parent) と等しくしたいと思います。

問題は、*layout_height* も "fill_parent" に設定すると、centerCrop が元の縦横比を保持するため、高さが優先され、画像が左右でトリミングされることです。(もちろん、これは幅よりも高さが高い画面を想定しています)。ただし、layout_height を「wrap_content」に設定すると、高さがスケーリングされなくなり、四角形が切り取られてしまいます。これは、画像の元の高さを尊重するためです。

「fill_parent」のlayout_widthを優先するlayout_height設定はありますか?

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="fill_parent"
    android:layout_height="?"
    android:layout_below="@id/header"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/cd_image"
    android:padding="0dp"
    android:scaleType="centerCrop"
    android:src="@drawable/blank_album" />

android:layout_height 属性を削除すると、クラッシュが発生します: java.lang.RuntimeException: Binary XML file line #16: You must supply a layout_height attribute.

android:layout_height="0dp" を設定すると、画像が垂直方向につぶれます。

回答: 他のユーザーからの回答を組み合わせて使用​​することで、自分に合った簡単な解決策を見つけることができました。

まず、イメージ ビューには次の構成があります ( fitStartに注意してください)。

<ImageView
    android:id="@+id/iv_album"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/header"
    android:layout_gravity="top"
    android:background="#FF00FF00"
    android:contentDescription="@string/app_name"
    android:padding="0dp"
    android:scaleType="fitStart"
    android:src="@drawable/blank_album" />

次に、プログラムで、幅に合わせて画像の高さを少し変更します (Activity.onResume() 内)。

        ImageView iv = (ImageView)findViewById(R.id.iv_album);
        boolean bPost = iv.post(
                new Runnable()
                {
                    public void run()
                    {
                        ImageView iv = (ImageView)findViewById(R.id.iv_album);
                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)iv.getLayoutParams();
                        params.width = ASong.this.findViewById(R.id.song_view_layout_top).getWidth();
                        params.height = params.width;                           
                        iv.setLayoutParams(params);
                    }
                });

        if (bPost == false)
        {
            throw new RuntimeException("runnable not posted");
        }
4

2 に答える 2

2

私は昨日この問題に対処しなければなりませんでした。これは私の問題を解決した XML です:

<ImageView
                android:id="@+id/character"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentBottom="true"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/ma_un1_001" />

ここのスレッドを参照してください:中央のドローアブルとスケールの高さ

手短に言うと:

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
于 2012-11-09T20:54:08.783 に答える
2

または、プログラムで実行できます。

ImageView を LinearLayout で囲む

<LinearLayout
android:id="@+id/container"
android:layout_below="@id/header"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <ImageView
    android:id="@+id/iv_image"
    android:layout_width="fill_parent"
    android:layout_height="?"
    android:contentDescription="@string/cd_image"
    android:padding="0dp"
    android:scaleType="centerCrop"
    android:src="@drawable/blank_album" />
</LinearLayout>

次に、onCreate() に挿入します。

findViewById(R.id.container).post(
    new Runnable() {
    public void run() {
        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)container.getLayoutParams();
        params.height = params.width;
        container.setLayoutParams(params);
    }}
);

メイン プロセスはレイアウト構造を直接変更できないため、コードは post() 内にある必要があります。

于 2012-11-09T23:20:16.087 に答える