使用android:adjustViewBounds=true
以XML爲ATTR ImageView
這個問題是通過擴展的ImageView類在這裏解決。
Android ImageView adjusting parent's height and fitting width
我創建了一個示例。這個對我有用。這是xml佈局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="@drawable/hydrangeas"
android:layout_height="wrap_content" android:id="@+id/imageView1"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="@android:color/white"
android:layout_width="wrap_content"/>
</LinearLayout>
它也適用於fitCenter
。看看快照。
感謝您的快速回答。你的意思是android:scaleType =「fitCenter」吧?與該選項和兩個維度fill_parent結果是類似於圖像3(圖像在中間)。例如,height =「wrap_content」的結果是圖像2的中間爲水平方向 – Addev
對於兩個參數都使用「wrap_content」,如果使用的是LinearLayout,則添加android:layout_weight =「1」 –