2012-10-21 21 views
24

Tôi muốn tùy chỉnh một số ImageButton không viền. Để tránh trùng lặp mã, tôi sử dụng styles.xml:styles.xml kế thừa "? 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> 

Tuy nhiên, các lỗi sau được ném:

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

Có cách nào để kế thừa borderlessButtonStyle?

Trả lời

-4

borderlessButtonStyle chỉ khả dụng ở cấp API 11 trở lên.

+9

Vẫn không hoạt động trên các API cao hơn, câu trả lời này có vẻ là sai – Sergii

+1

câu trả lời của aleung đang hoạt động, s o câu trả lời này không đúng, chỉ cần cung cấp thêm thông tin. – Seynorth

+2

vui lòng chấp nhận http://stackoverflow.com/a/30965291/883738 –

1

Tôi nghĩ bạn không thể kế thừa theo cách này. Hãy thử điều này:

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

Bạn không thể kế thừa thuộc tính, bạn phải kế thừa kiểu.

Chọn kiểu nút không đường viền khớp với chủ đề của bạn để kế thừa. Tất cả các kiểu nội bộ Android có thể được tìm thấy tại http://developer.android.com/reference/android/R.style.html

Ví dụ: ứng dụng của bạn sử dụng chủ đề Ánh sáng Holo (chỉ định trong AndroidManifest.xml), thì bạn nên kế thừa kiểu 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

Bạn có thể sử dụng phong cách này để thay thế cho API 11 trở lên:

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

Đối AppCompat, bạn có thể kế thừa từ phong cách cha mẹ:

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

Dựa trên source code

+3

Đây phải là câu trả lời được chấp nhận, do sử dụng AppCompat mới nhất –