2013-07-09 112 views
1

這是正在發生的事情:按鈕調整大小上> 2.3.6

button resizes horizontally in gt 2.3.6

這是源圖像:

source image

這是XML佈局的一部分:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background_headers" 
    android:orientation="horizontal" > 

    <AutoCompleteTextView 
     android:id="@+id/etNewItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:completionThreshold="2" 
     android:dropDownHeight="wrap_content" 
     android:dropDownWidth="wrap_content" 
     android:hint="@string/et_newitemhint" 
     android:inputType="text|textCapSentences" 
     android:layout_centerVertical="true" 
     style="@style/DialogEditText" 
     android:layout_marginRight="10dp" 
     android:singleLine="true" /> 


    <Button 
     android:id="@+id/btnAddNew" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/btn_add" 
     android:layout_centerVertical="true" /> 
</RelativeLayout> 

這是按鈕的選擇器xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_focused="true" 
    android:drawable="@drawable/btn_add_pressed"/> 
<item android:state_pressed="true" 
    android:drawable="@drawable/btn_add_pressed"/> 
<item android:drawable="@drawable/btn_add_normal"/> 
</selector> 

爲什麼此按鈕在4.x.x中水平調整大小?源圖形確實使用了9個補丁,但它做了完全相同的事情,所以我將其更改爲正常的png,但沒有任何結果。

奇怪的是,這是唯一顯示此行爲的圖形。我把它放在某個地方的一個操作欄上:同樣的事情;我也在對話中說過:同樣的事情。

我的2.2.2和2.3.6測試設備顯示按鈕應該是,但我的任何4.x.x測試設備顯示拉長的按鈕。

+0

只是在黑暗中刺,但你有沒有嘗試過使用'ImageButton'而不是'Button',並通過'android:src =「..」'而不是'android:background =「設置drawable。 「'?我想知道是否使用背景屬性不允許按鈕正確測量其大小,因此,奇怪的結果, –

+0

感謝您的建議@AlexMDC,您的解決方案可能已經工作,但我直接從androbat的代碼,它做了。 – DaveSav

回答

1

首先,使這些:

RES /繪製/ button_add_normal.xml

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/btn_add_normal" 
    android:tileMode="disabled" 
    android:gravity="top" > 
</bitmap> 

RES /繪製/ button_add_pressed.xml

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/btn_add_pressed" 
    android:tileMode="disabled" 
    android:gravity="top" > 
</bitmap> 

然後用此作爲選擇:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_focused="true" 
      android:drawable="@drawable/button_add_pressed"/> 
    <item android:state_pressed="true" 
      android:drawable="@drawable/button_add_pressed"/> 
    <item android:drawable="@drawable/button_add_normal"/> 
</selector> 
+0

謝謝!這在我的所有測試設備中都有效。 – DaveSav

0

我沒有設法在我的設備上重現此操作。不過,我還是有一個解決辦法:你可以使用精確的尺寸是這樣的:

android:layout_width="50dp" 
android:layout_height="50dp" 

通過這種方式,圖像看起來應當是一樣每個設備上。

+0

感謝您的建議,馬爾科姆;但是我表示我有這個圖形的ldpi,mdpi,hdpi和xhdpi分辨率的版本;所以設置一個固定的大小不是一個選項。 – DaveSav

+0

@DaveSav但是尺寸是以與密度無關的像素設置的,它會自動縮放。你甚至可以使用一個尺寸,併爲每個密度輸入不同的數字(儘管這對你的情況來說是一個過度殺傷力)。 – Malcolm

+0

真的嗎?但是,圖形的視覺質量會不會隨着它的擴大而降低?我給出的源代碼是用於mdpi;這個外觀如何擴展到xhdpi? – DaveSav