2014-02-22 177 views
0

我用來有一個相對佈局,我用一些ImageButtons排列。現在,因爲我有很多按鈕,我雖然會減少代碼大小,並動態地使用java代替靜態xml中的大部分內容。我創建了一個網格佈局,其中將元素ImageButtons與TextView一起引入。Android圖像按鈕奇怪的行爲

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="50" > 

<TextView 
    android:id="@+id/text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"/> 

<ImageButton 
    android:id="@+id/button_item" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/text_view" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/play_button" /> 
</RelativeLayout> 

我擴展BaseAdapter並傳遞一個列表項來填充gridview。它的工作原理確定,但使用的按鈕看起來不同(背景顏色):

before - after

另外,WRAP_CONTENT屬性是有點過了,因爲它切斷有點邊緣的底部和右側。 任何想法可能導致這種情況?

+0

使用ImageView如果你沒有任何文字it.it可能會解決你的問題。 – Manmohan

+0

使用ImageView設置'android:scaleType'爲'ImageButton' –

+0

@Manmohan避免了這個問題,我想知道爲什麼會發生這種情況..!但如果我找不到爲什麼我會恢復到這個選項。 – tk66

回答

0

解決了!問題是,當我創建包含按鈕的自定義fragmentPagerAdapter時,我傳遞了由getApplicationContext()檢索的上下文。我將它切換到getBaseContext(),現在按預期工作。