[英]Fit a ImageView (and its src) to the layout width and make its height proportional
你能幫我配置我的布局嗎? 我沒有達到我正在尋找的東西:
我已將 imageview 背景設為紅色以解釋我想要的內容:
圖像的寬度小於布局寬度。 我希望圖像按比例增長,保持其縱橫比,直到達到第一張圖像的縱橫比(我用 photoshop 手動修改了它)但是當設置 width="fill_parent" 時結果是第二張圖像並將寬度和高度都設置為“ fill_parent" 結果是第三張圖片。
我嘗試與 ScaleType 結合但沒有成功
我怎樣才能實現第一個選項? 謝謝
使用android:adjustViewBounds=true
attr 在 xml 為ImageView
這個問題在這里通過擴展 ImageView class 來解決。
我創建了一個示例。 這個對我有用。 這是 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
。 看看快照。
在您的 ImageView 標簽中使用屬性 fit_center。
<ImageView ... android:scaleType="fitCenter" .. />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.