17

画像にオーバーレイするためにrelativelayoutを使用しています。これまでにテストしたすべての画面サイズ (2.7 から 10.1 インチまで) で、画像の上に常に空白が表示されます。私の IDE では、relativelayout が原因で画像の上下に余分なスペースが生じていることが常にわかります。

何故ですか?すべての高さ属性を設定しwrap_content、属性を追加しましたadjustViewBounds

注: 私の画像はサイズがかなり大きいことを知っておく必要があります。つまり、何らかのスケーリングがあることを意味します。

ヒントをありがとう!

コードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" >

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" 
    android:focusable="true"
android:focusableInTouchMode="true">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@string/bgf"
    android:textColor="#000000"
    android:textSize="25sp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:adjustViewBounds="true"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="5dp"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/cde"
        android:contentDescription="@string/cde" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_gravity="center_horizontal"
        android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
        android:src="@drawable/fgh"
        android:contentDescription="@string/abc" />

</RelativeLayout>


</LinearLayout>
</ScrollView>
4

2 に答える 2

52

私は正確な問題を抱えていました。かなり苦労した後、これに従って解決しまし

そして私のプログラムに以下を追加してください

 android:adjustViewBounds="true"

それは完璧に動作します

于 2014-11-10T09:20:57.600 に答える
15

これは、画像のアスペクト比を失うことなく、使用可能な領域に合わせて画像が縮小されるときに発生します。画像が最初に使用可能な幅を占有し、元の画像の縦横比に従って高さが縮小されたため、空白ができています。

より明確な理解を得るために、次のことをお勧めします。

  • 相対レイアウトの背景色を特定の色に設定する

これで、imageView と relativelayout の間のスペースを理解できます。

デバイスでそれを確認したら、次の変更を行い、もう一度やり直してください

  • imageView で属性 scaleType = "fitXY" を設定します。

これにより、画像が拡大縮小され、imageView で使用できる領域全体を占有します。(この場合、元の画像の縦横比は失われます)。これで、imageView が占める領域の量がわかるようになります。

これを行うと、次のように結論付けることができると思います。

  1. relativeLayout の背景を黒に設定した場合、最初の実行では、imageView が隙間を残さずに領域全体を占有するため、表示されません。

  2. 2 回目の実行では、縦横比は失われますが、画像は高さと幅全体をカバーします。したがって、これは imageView が幅と高さをカバーし、スペースが残っていないことを確認します。これは画像のアスペクト比、つまり問題です

まったく異なる結果に到達した場合はお知らせください。解決いたします。

編集 :

imageView に指定したパディングも削除してください

于 2013-07-31T10:24:42.857 に答える