2012-10-21 47 views
24

我想定製一些無邊界的ImageButtons。爲了避免重複代碼,我用styles.xmlstyles.xml繼承「?android:attr/borderlessButtonStyle」?

<style name="EditNotesButton" parent="?android:attr/borderlessButtonStyle"> 
    <item name="android:layout_width">25dp</item> 
    <item name="android:layout_height">25dp</item> 
    <item name="android:scaleType">fitCenter</item> 
</style> 

但是,下面的錯誤被拋出:

Error retrieving parent for item: No resource found that matches the given name '?android:attr/borderlessButtonStyle'. 

有沒有辦法繼承borderlessButtonStyle

回答

-4

borderlessButtonStyle僅在API級別11或更高版本上可用。

+9

仍然不能在更高的API上工作,這個答案似乎是錯誤的 – Sergii

+1

aleung answer is working,s這個答案不是正確的,只是提供了額外的信息。 – Seynorth

+2

請接受http://stackoverflow.com/a/30965291/883738 –

1

我認爲你不能以這種方式繼承。試試這個:

<style name="EditNotesButton" parent="@android:style/Widget"> 
    <!-- This will cause borderless button --> 
    <item name="android:background">@android:drawable/list_selector_background</item> 
    ... 
</style> 
36

你不能繼承一個屬性,你必須繼承一個樣式。

選擇與您的主題相匹配的按鈕無邊框樣式來繼承。所有Android內部樣式可以在http://developer.android.com/reference/android/R.style.html

例如,您的應用程序使用Holo Light主題(在AndroidManifest.xml中指定),那麼您應該繼承Widget.Holo.Light.Button.Borderless.Small樣式。

<style name="EditNotesButton" parent="@android:style/Widget.Holo.Light.Button.Borderless.Small"> 
    <item name="android:layout_width">25dp</item> 
    <item name="android:layout_height">25dp</item> 
    <item name="android:scaleType">fitCenter</item> 
</style> 
6

您可以使用這種風格,而不是爲API 11以上:

<style name="EditNotesButton" parent="android:Widget.Holo.Button.Borderless" /> 
50

對於程序兼容性,可以從父樣式繼承:

<style name="MyBordelessButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless"> 
     <item name="android:textSize">@dimen/<your size></item> 
     <!-- other items here --> 
</style> 

基礎上source code

+3

這應該是被接受的答案,由於使用最新的AppCompat –