创建自定义按钮类以在Android中设置样式

问题描述 投票:0回答:1

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

我想对所有textviews和editText使用此方法,但是我找不到如何执行此操作。请建议方法。谢谢。

android android-layout android-appcompat android-button material-components-android
1个回答
0
投票

这并非您所要的。使用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>
© www.soinside.com 2019 - 2024. All rights reserved.