【问题标题】:Create custom button class to set style in Android创建自定义按钮类以在 Android 中设置样式
【发布时间】:2020-02-01 04:34:14
【问题描述】:

我想创建一个自定义按钮类,这样我就不需要总是在 xml 中添加样式。我有时会想念在 xml 中添加样式,并且是作为错误创建的。通过使用自定义类,我想从styles.xml 中删除每次添加样式的依赖。

我想对所有 textview 和 editText 使用这种方法,但我找不到如何做到这一点。请建议方法。谢谢。

【问题讨论】:

    标签: android android-layout android-appcompat android-button material-components-android


    【解决方案1】:

    这不是您要找的。
    使用 Material Components library 并在您的应用主题中使用您喜欢的样式定义 materialButtonStyle 属性。
    它将在应用程序中全局定义按钮的样式。

    <style name="AppTheme" parent="Theme.MaterialComponents.*">
       ...
       <item name="materialButtonStyle">@style/CustomButton</item>
    </style>
    
    <style name="CustomButton" parent="@style/Widget.MaterialComponents.Button">
      ...
    </style>
    

    如果您仍在使用 AppCompat 主题,则可以使用 buttonStyle 属性。

    <style name="AppTheme" parent="Theme.AppCompat.*"/>
       ...
       <item name="buttonStyle">@style/Widget.AppCompat.Button</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      相关资源
      最近更新 更多