Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit Android style attribute?

I've recently tried adopting a style-based approached to my android app. However, I'm stuck with this dilemma.

In my layout file I originally had this:

 <LinearLayout
         android:id="@+id/layout_event_buttons"
         style="?android:attr/buttonBarStyle"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:orientation="horizontal" >

         <ImageButton
             android:id="@+id/btn_create_event"
             style="?android:attr/buttonBarButtonStyle"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:contentDescription="@string/btn_save"
             android:src="@drawable/content_save"
             android:text="@string/btn_save" />
</LinearLayout>

In my styles.xml I tried doing this with no luck:

<style name="Buttons" parent="?android:attr/buttonBarButtonStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>

I already tried different variations of the above like:

?android:buttonBarButtonStyle

?attr:buttonBarButtonStyle

@android:attr/buttonBarButtonStyle

@android:buttonBarButtonStyle

I also tried:

<item name="style">?android:attr/buttonBarButtonStyle</item> 

to no avail.

My app won't compile because it can't find the buttonBarButtonStyle resource.

This is how I applied it. My other styles work fine if I just inherit the common themes like Holo, etc.

<ImageButton
     style="@style/Buttons"
     android:id="@+id/btn_create_event"
     android:contentDescription="@string/btn_save"
     android:src="@drawable/content_save"
     android:text="@string/btn_save" />
like image 841
Chad Avatar asked Sep 25 '13 19:09

Chad


People also ask

How to add styles XML file in Android studio?

Create and apply a style To create a new style or theme, open your project's res/values/styles. xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style.

Where is my styles XML in Android Studio?

Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file.

What is attr in Android?

attr/ references to attributes. Attributes are values specified in an app's theme. The attributes in your example are all values specified in the themes provided by the support library. Android also has its very own attributes which can be used with ? android:attr/ .

What is TextAppearance in Android?

TextAppearance styles can be seen as the Android equivalent of Material Design type styles. For custom styles, we recommend two approaches to help separate concerns and create a single source of truth for type theming values in your app: Store all TextAppearance styles in a single res/values/type. xml file.


1 Answers

Found the answer to be similar to this post: How does an android:attr style work?

Turns out this style is private and you may need to copy it completely to be able to pull it off. :(

like image 146
Chad Avatar answered Sep 28 '22 14:09

Chad