Android覆盖预定义的父样式
我使用MaterialButton的一种预定义样式Android覆盖预定义的父样式,android,android-styles,Android,Android Styles,我使用MaterialButton的一种预定义样式 <com.google.android.material.button.MaterialButton style="@style/Widget.MaterialComponents.Button.OutlinedButton" android:layout_width="wrap_content" android:layout_height="wrap_content" an
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
现在我试图覆盖mystyles.xml中的Widget.MaterialComponents.Button.TextButton
的一个属性
<style name="Widget.MaterialComponents.Button.TextButton">
<item name="android:textColor">@color/colorAccent</item>
</style>
@颜色/颜色重音
我想知道这是否是覆盖预定义样式的正确方法,并将样式
Widget.MaterialComponents.Button.OutlinedButton
使用@color/colorAccent
作为文本颜色。我认为这是可行的,但我不能确定这种方法是否有副作用,或者是否有更好的方法?您可以使用以下方法之一:
<style name="MyCustomStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:textColor">@color/colorAccent</item>
</style>
@颜色/颜色重音
@颜色/颜色重音
使用这些方法,您将继承父样式的所有属性,并且只声明要更改/覆盖的属性
并在布局文件中使用此样式,如下所示:
<com.google.android.material.button.MaterialButton
style="@style/MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
或
我建议您定义自己的风格并继承所需的小部件。但在这种情况下,我必须重新定义所有继承的样式。我只想使用现有的样式进行一些自定义。这就是它所做的,如果您使用父级定义样式,则定义的样式将继承它的所有内容。我知道您可能不想去修改元素使用的所有样式,但这是最干净的解决方案。请发布布局xml代码和样式xml代码好吗?除了我的回答之外,您需要在布局文件中使用此自定义样式:``````
<style name="MyCustomStyle" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:textColor">@color/colorAccent</item>
</style>
<style name="Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle">
<item name="android:textColor">@color/colorAccent</item>
</style>
<com.google.android.material.button.MaterialButton
style="@style/MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton.MyCustomStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve" />