Home » Android » styles.xml inherit "?android:attr/borderlessButtonStyle"?

styles.xml inherit "?android:attr/borderlessButtonStyle"?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’d like to customize some borderless ImageButtons. To avoid code duplication, I use styles.xml:

<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>

However, the following error is thrown:

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

Is there any way to inherit borderlessButtonStyle?

How to&Answers:

borderlessButtonStyle only available on API level 11 or above.

Answer:

For AppCompat, you can inherit from the parent style:

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

Based on the source code

Answer:

You can’t inherit an attribute, you have to inherit a style.

Choose a button borderless style which matchs your theme to inherit. All Android internal styles can be found at http://developer.android.com/reference/android/R.style.html

For example, your application uses Holo Light theme (specify in AndroidManifest.xml), then you should inherit Widget.Holo.Light.Button.Borderless.Small style.

<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>

Answer:

You can use this style instead for API 11 and above:

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

Answer:

I think you can’t inherit in this way. Try this:

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