2015-02-09 91 views
0

我擁有最新的Android Studio和SDK(編譯SDK 21,構建工具21.1.2,appcompat v7 21.0.3)並使用空白活動模板創建一個全新的嚮導應用程序。Android按鈕在Jellybean上是半透明的,但不在Lollipop上

然後我只更改XML:根相對佈局(android:background =「#00FF00」)的背景並添加一個按鈕(使用圖形編輯器,不更改按鈕)。

這適用於棒棒糖設備。在糖果軟糖(4.2.2)設備或模擬器上,該按鈕以灰色呈綠色觸摸呈現。

這可能是一個主題(Theme.AppCompat.Light.DarkActionBar),但它應該不會在Lollipop和Jellybean設備上呈現相同的內容嗎?

我看過關於手動更改按鈕背景的問題(例如How to make button non transparent),這是一個兼容性問題。

以下是完整的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
android:background="#00FF00"> 

<TextView android:text="@string/hello_world" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:layout_below="@+id/textView" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginLeft="26dp" 
    android:layout_marginStart="26dp" 
    android:layout_marginTop="55dp" /> 

</RelativeLayout> 

回答

0

如果不指定style或添加android:background="yourBackground"到您的按鈕的XML,Android將呈現您的按鈕,Android版本的默認按鈕樣式您應用正在運行。

例如,如果您將android:background="#FF0000"添加到您的按鈕,那麼不同的android版本就不會有很大的差異。

1

AppCompat當前不支持Button小部件,因此Holo主題(API級別小於21)上的按鈕呈現與Material主題(API Levels 21+)不同。你需要定義一個按鈕的風格是這樣的:在res

<style name="MyButtonStyle" parent="Base.MyButtonStyle"/> 

然後/值目錄,你定義Base.MyButtonStyle

<style name="Base.MyButtonStyle" parent="android:Widget.Holo.Button"> 
    <item name="android:background">@drawable/btn_default</item> 
</style> 

的btn_default繪製將指向爲您的自定義選擇按鈕。

在res /值-V21定義Base.MyButtonStyle這樣的:

<style name="Base.MyButtonStyle" parent="android:Widget.Material.Button"/> 

如果要更改按鈕的正常或按顏色21+您將設置colorButtonNormal和colorControlHighlight在主題,分別。

+0

有趣的方法。一個人必須使用「@android:drawable/btn_default」,但它的工作原理。 – 2015-04-04 08:10:18

相關問題