2017-05-26 20 views
1

在LinearLayout中,我試圖使用數字圖像顯示整數。我希望數字居中,圖像的大小取決於有多少數字。使用LinearLayout顯示可變數量的圖像

我最初的佈局與2個數字:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 

<view 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" 
    android:gravity="center" 
    > 
    <ImageView 
     app:srcCompat="@drawable/ic_digit_0" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:adjustViewBounds="true" 
     android:layout_weight="1" 
     /> 
    <ImageView 
     app:srcCompat="@drawable/ic_digit_0" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:adjustViewBounds="true" 
     android:layout_weight="1" 
     /> 
</LinearLayout> 

這裏是什麼樣子。

A)4位數,一切看起來不錯,數字水平適合,儘可能垂直。

4 digits scaled to fit

B)多了很多數字,但它仍然很好地擴展到可用空間。

many more digits scale nicely

C)但是,當的LinearLayout高度減小時,4位分離和間隙在它們之間出現。

images have separated

d)通過從每個的ImageView除去layout_weight中,4位回到一起。

digits come back together

E)但是,當的LinearLayout高度增加,數字刻度到視圖的整個高度和水平一些消失關閉屏幕。

digits too large

我怎麼能有這些數字圖像調整大小以填充可用空間,顯示所有的數字和無間隙爲中心,同時保留其規模的比例,無論的LinearLayout尺寸的?

謝謝。

+0

已嘗試android:scaleType根據您的要求嘗試其屬性 – Pavan

+0

給定imageview的固定大小!使用scaletype- – xbadal

+0

scaleType似乎不起作用。 fitXY拉伸或壓縮圖像,使其比例改變比例。修復圖像大小需要根據數字位數進行代碼計算。 –

回答

0

您是否嘗試添加scaletype?

<ImageView android:scaleType="scaleXY" android:src="@drawable/my_image"/> 

https://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

+0

? https://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType – Pavan

+0

不,它只是一個例子。請參閱scaletypes:https://developer.android.com/reference/android/widget/ImageView.ScaleType.html –

+0

我已經嘗試過android:scaleType的各種組合,但它們都不起作用。使用** fitXY **會導致數字超出範圍。 –

0

確定。我找到了答案。從 「match_parent」 到 「WRAP_CONTENT」 layout_width =:

對於第二的LinearLayout,所述機器人改變。然後添加android:layout_gravity =「center」。這允許LinearLayout收縮到其較小的高度迫使數字收縮的內容。但是,當它縮小的時候,它也是以包含的觀點爲中心。

感謝您輸入的每個人。