Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android XML, can I set buttonStyleSmall or Button.Small as a parent style to a custom style?

I want to extend Android's small button style. I can do it inline:

<Button android:id="@+id/myButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="10dp"
        android:text="Click Here"/>

But in the interest of reusability, I want to transfer these styles to a custom style. How can I add buttonStyleSmall (or Widget.Button.Small?) as a parent to a style? Something like this in my custom style XML:

<style name="RightLink" parent="?android:attr/buttonStyleSmall">
  <item name="android:layout_alignParentRight">true</item>
  <item name="android:layout_gravity">right</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_width">wrap_content</item>
  <item name="android:paddingLeft">2dp</item>
  <item name="android:paddingRight">2dp</item>
  <item name="android:textSize">10dp</item>
</style>

with the button declaration using that style:

<Button android:id="@+id/myButton"
        style="@style/RightLink"
        android:text="Click Here"/>

EDIT

Using the correct syntax as described by Lukas below (use @android:attr/buttonStyleSmall as the parent), I'm still seeing a difference between the two:

Button with buttonStyleSmall as style and inline styles added:

pic1

Custom style with buttonStyleSmall as the parent:

pic2

What am I missing?

like image 991
annie Avatar asked Aug 23 '11 19:08

annie


1 Answers

So you try to inherit style informations from a standard Android style. For this, you'll need to use the parent-attribute as you already did.

The only exception when inheriting a standard style is, that you have to use an @ not a ?:

<style name="RightLink" parent="@android:attr/buttonStyleSmall">

A little more about how to do that for platform styles (because that differs from styles you created yourself) can be found here.


After playing around with it i found that the way you inhirate is correct, but the recourse is wrong:

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

This works.

The difference seams to be that the integer-constant which is used to add this by using the style-attribute and in code is declared in Androids R-class in the attr-subclass.

The XML-Style definitions (from which you can actually inherit) are stored in the style-subclass of the R-class. So the above line should solve your problem.

like image 100
Lukas Knuth Avatar answered Oct 02 '22 13:10

Lukas Knuth