2016-06-22 25 views
3

我有兩個Buttons嵌套在LinearLayout。這兩個Buttons之間是兩個TextViews。在Xml中,我已將前景設置爲這些Buttons中的每一個的圖像。如何使按鈕在API 23下工作的前景屬性?

它運行良好,在我的設備上Api 23。但在Api 23以下的其他設備上,前景圖像不會顯示,而是會導致默認的白色純色。有沒有什麼辦法讓這些圖像在Api 23以下使用前景顯示?

我們嘗試過FrameLayout,但它沒有做到我們希望它做的事情。 ImageButtons是解決此問題的更好方法嗎?

我們的應用程序的核心功能之一是,每當用戶點擊一個Button,大小增加,圖像相應地延伸。這是在代碼中動態完成的。如果我要使用ImageButtons,則需要每次設置高度和寬度的佈局參數,而不是設置高度的一行代碼。

任何提示將不勝感激!

編輯:代碼我一起工作 -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="11" 
    android:background="@android:color/black"> 

    <Button 
     android:layout_weight="5" 
     android:id="@+id/firstP" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:foreground="@drawable/icebutton" 
     android:scaleX="1" 
     android:scaleY="1"/> 

    <TextView 
     android:layout_weight="0.5" 
     android:id="@+id/firstPlayer" 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:rotation="180" 
     android:textColor="@android:color/white" 
     android:background="@android:color/transparent"/> 

    <TextView 
     android:layout_weight="0.5" 
     android:id="@+id/secondPlayer" 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/white" 
     android:background="@android:color/transparent"/> 

    <Button 
     android:layout_weight="5" 
     android:id="@+id/secondP" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:foreground="@drawable/firebutton" 
     android:scaleX="1" 
     android:scaleY="1"/> 

</LinearLayout> 
+0

請問您可以向我們展示您的代碼! –

+0

我已經添加了我正在使用的代碼。我目前正試圖實現'ImageButtons' @Micho感謝您的編輯。我將確保我將來的格式正確。 – Kumar

回答

3

我們發現,有導致不顯示圖像的兩個問題。
1.圖像文件的大小過大,產生outOfMemory錯誤,從而導致按鈕不顯示圖像。
2.前景屬性不適用於API 22及更低版本。

解決這些問題的步驟:
1.我們減小了圖像文件的大小。
2.我們與ImageButton
3.更換ButtonXML文件我們去掉了前景屬性,增加了一個黑色的背景,並通過src屬性添加的圖像。以下是一個片段。

<ImageButton 
    android:layout_weight="5" 
    android:id="@+id/firstP" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top" 
    android:src="@drawable/icebutton" 
    android:scaleType="fitXY" 
    android:background="@android:color/black"/> 
  • 然後,我們不得不改變我們的代碼,以動態調整按鈕的高度通過設置LayoutParams,以配合該鏈接的幫助下,新的圖像按鈕:
    how to change size of button dynamic in android
  • 現在一切都完美了!

    +1

    API 22!這是我的問題。正在運行21測試。謝謝。 – LimaNightHawk