我想创建一个自定义按钮类,这样就不必总是在xml中添加样式。我想念有时在xml中添加样式,并且被创建为错误。通过使用Custom类,我希望每次都从styles.xml
中删除添加样式的这种依赖性。
我想对所有textviews和editText使用此方法,但是我找不到如何执行此操作。请建议方法。谢谢。
这并非您所要的。使用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>