Android ImageView 圖片靠右,靠左處理

Thinan發表於2016-06-21

ImageView 圖片靠右,靠左處理

相信在工作中很多人都會遇到ImageView需要圖片靠左和靠右,典型的案例就是懸浮窗縮排的小圖片,前幾天在工作中遇到,隨手一記。

  • 簡單介紹下佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/button6"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ourpalm_float_half_left"
        />

</LinearLayout>

這個時候如果不做任何處理,而且想讓圖片靠左的話,實際的顯示效果是這樣的

這裡寫圖片描述

到這裡腦子突然卡住了,好久沒調這個,然後經過研究發現:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/button6"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ourpalm_float_half_left"
        android:scaleType="fitStart"
        />

</LinearLayout>
  • 這個時候會驚喜的發現

這裡寫圖片描述

  • 加入android:scaleType=”fitStart” 後,圖片乖乖的靠左,查了安卓的文件:
    android:scaleType=”fitStart” 把圖片按比例擴大(縮小)到View的寬度,顯示在View的上部分位置。關鍵字:按比例縮放,靠左顯示全圖,
    很簡單的解決問題,
  • 如果靠右的話,android:scaleType=”fitEnd” 加入這個就可以了,至於這個懸浮窗是在左還是在右顯示,就需要程式碼邏輯判斷了,這個就不多說了。

    是不是很簡單的解決了問題,android的確勃大莖深,有時候很簡單的配置解決複雜的問題,順手一記,老司機輕噴.

相關文章