我想使用styles.xml在布局中设置按钮的样式,如下所示。
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="SearchButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">15sp</item>
<item name="colorButtonNormal">@color/colorPrimary</item>
</style>
并使用android:theme将此样式应用于按钮。
<Button
android:theme="@style/SearchButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="15dp"
android:text="@string/search_button_text" />
在预览窗口中,此代码不会对默认灰色“材质设计”按钮进行任何更改。
EXTRA:我尝试使用“style =”并且它可以正常工作,但它忽略了colorButtonNormal并使用了accentColor。
“彩色”按钮从styles.xml文件中获取colorAccent属性的颜色。将colorAccent的颜色更改为您选择的颜色以获得所需的背景颜色。
colorButtonNormal仅适用于具有默认样式的按钮。
你可以尝试这个answer。