How to define button background at one place and used around the whole app?

后端 未结 2 1903
感情败类
感情败类 2021-01-18 03:29

I would like all the buttons of my android application use the same background image, that\'s all buttons have attribute:

android:background         


        
相关标签:
2条回答
  • 2021-01-18 03:39

    Define styles.xml with the below:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <style name="buttonStyle">
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:background">@drawable/bg_button</item>
        </style>
    </resources>
    

    And use the same style for any button within your application as:

    <Button 
        android:text="Test Button" 
        android:id="@+id/btnTest" 
        style="@style/buttonStyle">
    </Button>
    
    0 讨论(0)
  • 2021-01-18 03:57

    For Android styles, you reference the preset attributes of R.attr. Here, you want to to reference android:buttonStyle. You can try this :

    <style name="ApplicationStyle" parent="android:Theme">
    <item name="android:buttonStyle">@style/yourButton</item>
    </style>
    

    Also look in this Themes and Styles

    0 讨论(0)
提交回复
热议问题