2017-10-04 64 views
-1

我用Button,TextView和ImageView創建了一個簡單的佈局(帶有問號)。如何將ImageView對齊到按鈕的右邊緣?

<Button 
     android:background="#f00" 
     android:textSize="25dp" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Button" 
     android:id="@+id/button" /> 


    <TextView 
     android:id="@+id/textViewmas" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button" 
     android:layout_alignLeft="@+id/button" 
     android:layout_alignStart="@+id/button" 
     android:text="title " 
     android:textSize="18dp" /> 

    <ImageView 
     android:id="@+id/questionmark" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@id/textViewmas" 
     android:layout_alignTop="@+id/textViewmas" 
     android:layout_alignRight="@+id/button" 
     android:layout_alignEnd="@+id/button" 
     app:srcCompat="@drawable/questionmark"/> 

enter image description here

The question mark should be a bit more right

我想要的圖像的右邊緣(問號)在按鈕的右邊緣對齊。 額外的頂部和底部應該在標題的textView(相同高度)的頂部和底部對齊。

爲什麼圖像沒有在右邊對齊,但剩下多一點? 我注意到,如果我增加標題文字大小(例如30dp),問題消失,但我需要一個小的文字大小。

------編輯:是否有圖像的最小尺寸,在這種情況下,報表不能正常工作?

+0

什麼ü究竟想ü可以使圖像在如果你想要的話,我會爲你準備好你想要的。 –

+0

確保圖像沒有任何額外的填充。如果圖像中有任何額外的填充,請使用photoshop或任何其他工具將其刪除。或者,檢查這個圖像,看看它是否適合。 https://image.flaticon.com/icons/png/512/78/78299.png – Aveek

+0

我已經發布了一個解決方案。讓我知道它是否有效 –

回答

1

添加android:scaleType="fitEnd"ImageView

<Button 
    android:id="@+id/button" 
    android:layout_height="wrap_content" 
    android:layout_width="200dp" 
    android:textSize="25dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="#f00" 
    android:text="Button"/> 

<TextView 
    android:id="@+id/textViewmas" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button" 
    android:layout_alignLeft="@+id/button" 
    android:text="title " 
    android:textSize="18dp" /> 

<ImageView 
    android:id="@+id/questionmark" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@id/textViewmas" 
    android:layout_alignTop="@+id/textViewmas" 
    android:layout_alignRight="@+id/button" 
    android:layout_alignEnd="@+id/button" 
    android:scaleType="fitEnd" 
    app:srcCompat="@mipmap/ic_launcher"/> 

說明

這是怎麼ImageView似乎沒有android:layout_alignTop="@+id/textViewmas"屬性

enter image description here

當您在dd layout_alignTop屬性到ImageView,它被壓縮到TextView的高度,並且它包含的圖像被縮小。默認情況下,ImageViewscaleType屬性顯示爲centerInside;它縮小圖像並將其與圖像視圖的中心對齊,從而在左側和右側添加一些空間。

的解決辦法是使用android:scaleType="fitEnd"到縮小圖像移動到圖像視圖

輸出結束

enter image description here

+0

謝謝,最後它的作品! – user7940193

0

您可以使用:

android:layout_alignRight="@+id/button" 

或:

android:layout_alignEnd="@+id/button" 

,然後你可以從左邊添加一些餘量。

+0

我的代碼中已經添加了兩個對齊語句。保證金沒有改變。 – user7940193

+0

然後,您可以獲取文字的高度並將其設置爲圖像的高度和高度。 –