2013-02-03 64 views
2

This 不起作用,因爲沒有buttonStyle項目名稱。它只有button如何將樣式應用於Android應用程序中的所有按鈕?

我的例子:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <style name="AppTheme" parent="@android:style/Theme.Black"> 
     <item name="android:button">@style/ThemeButton</item> 
    </style> 

    <style name="ThemeButton" parent="@android:style/Widget.Button"> 
     <item name="android:background">@drawable/button</item> 
    </style> 

</resources> 

但是,這也不行。

+0

是的,有按鈕樣式的項目。你確定你在你的應用程序中使用Theme.Black嗎? –

+0

是的。但沒有buttonStyle,看到我的屏幕:http://s43.radikal.ru/i100/1302/f1/012f48561ebd.png –

+5

沒有關係,eclipse沒有顯示它。它在那裏,嘗試輸入並運行你的應用程序。 –

回答

6

對不同API級別使用不同主題時,我會覆蓋每個主題的按鈕樣式。考慮下面的例子。

您的應用程序的API 14+的主題是一個派生自android:Theme.Holo.Light.DarkActionBar的主題。

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
    <!-- API 14 theme customizations can go here. --> 
</style> 

android:Theme.Holo.Light.DarkActionBar沒有自己的buttonStyle,但其父Theme.Holo.Light一樣。和按鈕樣式如下:

<item name="buttonStyle">@style/Widget.Holo.Light.Button</item> 

但自己的應用API主題21所android:Theme.Material.Light.DarkActionBar

<style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar"> 
    <!-- API 21 theme customizations can go here. --> 
</style> 

導出的android:Theme.Material.Light的按鈕樣式如下:

<item name="buttonStyle">@style/Widget.Material.Light.Button</item> 

Widget.Button是我上面提到的Widget.Holo.Light.ButtonWidget.Material.Light.Button的祖父。因此,如果您在應用程序中從Widget.Button派生,則會失去Google工程師爲Widget.Material.Light.ButtonWidget.Holo.Light.Button提供的自定義設置。

我記錄了幾個例子來證明這一點。

Android按鈕樣式。 Widget.Button適用於所有API級別。

老式按鈕所有API級別 http://youtu.be/hKWUFgvw-Gs

styles.xml的文件夾中看起來是這樣的:

<style name="AppBaseTheme" parent="android:Theme.Light"> 
</style> 

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 

    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    <item name="android:buttonStyle">@style/Button</item> 
</style> 

<style name="Button" parent="@android:style/Widget.Button"></style> 

每個按鈕樣式從的按鈕樣式派生其主題。

的按鈕具有材質動畫的文件夾中http://youtu.be/8Wp1TWjjha0

styles.xml 值-V21看起來是這樣的:

<resources> 

    <style name="AppBaseTheme" parent="android:Theme.Material.Light.DarkActionBar"> 
     <!-- API 14 theme customizations can go here. --> 
    </style> 

    <style name="BaseButton" parent="@android:style/Widget.Material.Light.Button"></style> 

</resources> 

styles.xml的文件夾中值-V14外觀像這樣:

<resources> 
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
    </style> 

    <style name="BaseButton" parent="@android:style/Widget.Holo.Light.Button"></style> 

</resources> 

樣式。XML在文件夾看起來是這樣的:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <style name="AppBaseTheme" parent="android:Theme.Light"></style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <item name="android:buttonStyle">@style/Button</item> 
    </style> 

    <style name="Button" parent="@style/BaseButton"> 
     <item name="android:text">Lorem Ipsum</item> 
    </style> 

</resources> 

Material Design for Android 5.0 (API level 21)

How to record your screen on Android 4.4.4

Video bitrates

+0

爲什麼停止使用API​​級別16?我無法爲所有按鈕設置樣式 – Michael

+0

@Michail,您可以發佈您的代碼嗎?例如,在Github上 –

相關問題