如何覆盖 android 视图中的样式属性
How to override attributes of a style in a android view
我想知道如何覆盖引用样式的样式属性。通过简单地放置属性来完成它似乎不是一个解决方案 - 请参见 insetLeft
。 属性 是 material 按钮视图本身的一部分,但设置 属性 对样式没有影响。
我的布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="true"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:orientation="horizontal"
android:insetLeft="0dp"
android:insetRight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/serverIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/serverLastContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
对于这种情况,您可以按照以下步骤操作:
- 转到 styles.xml 并创建一个父级为“Widget.MaterialComponents.Button.TextButton”的新样式。示例:
<style name="CustomButtonLayout" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
</style>
- 在您的 xml 视图组中使用创建的样式。示例:
style="@style/CustomButtonLayout"
显式声明属性会覆盖样式中的声明。
但是,您正在将视图样式应用于布局,并非所有属性都会对该视图的绘制产生影响。
在我看来 insetLeft
不是您引用的样式的一部分 Widget.MaterialComponents.Button.TextButton
。让我们通过在每个样式引用上输入 Ctrl-B 来提升层次结构。
这是样式定义i material-1.4.0 values.xml:
</style>
<style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textColor">@color/mtrl_text_btn_text_color_selector</item>
<item name="android:paddingLeft">@dimen/mtrl_btn_text_btn_padding_left</item>
<item name="android:paddingRight">@dimen/mtrl_btn_text_btn_padding_right</item>
<item name="iconTint">@color/mtrl_text_btn_text_color_selector</item>
<item name="iconPadding">@dimen/mtrl_btn_text_btn_icon_padding</item>
<item name="backgroundTint">@color/mtrl_btn_text_btn_bg_color_selector</item>
<item name="rippleColor">@color/mtrl_btn_text_btn_ripple_color</item>
</style>
现在,让我们看看“Widget.MaterialComponents.Button.UnelevatedButton”:
<style name="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:stateListAnimator" ns1:ignore="NewApi">@animator/mtrl_btn_unelevated_state_list_anim</item>
<item name="elevation">0dp</item>
</style>
层次结构到此结束。我没有看到 insetLeft
属性在任何地方被引用,所以你不能在你的布局中覆盖它 - 至少通过你的布局中的 style
引用。
说明您正在尝试做什么以获得可能有帮助的答案。
我想知道如何覆盖引用样式的样式属性。通过简单地放置属性来完成它似乎不是一个解决方案 - 请参见 insetLeft
。 属性 是 material 按钮视图本身的一部分,但设置 属性 对样式没有影响。
我的布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="true"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:orientation="horizontal"
android:insetLeft="0dp"
android:insetRight="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/serverIp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/serverLastContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
对于这种情况,您可以按照以下步骤操作:
- 转到 styles.xml 并创建一个父级为“Widget.MaterialComponents.Button.TextButton”的新样式。示例:
<style name="CustomButtonLayout" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
</style>
- 在您的 xml 视图组中使用创建的样式。示例:
style="@style/CustomButtonLayout"
显式声明属性会覆盖样式中的声明。
但是,您正在将视图样式应用于布局,并非所有属性都会对该视图的绘制产生影响。
在我看来 insetLeft
不是您引用的样式的一部分 Widget.MaterialComponents.Button.TextButton
。让我们通过在每个样式引用上输入 Ctrl-B 来提升层次结构。
这是样式定义i material-1.4.0 values.xml:
</style>
<style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textColor">@color/mtrl_text_btn_text_color_selector</item>
<item name="android:paddingLeft">@dimen/mtrl_btn_text_btn_padding_left</item>
<item name="android:paddingRight">@dimen/mtrl_btn_text_btn_padding_right</item>
<item name="iconTint">@color/mtrl_text_btn_text_color_selector</item>
<item name="iconPadding">@dimen/mtrl_btn_text_btn_icon_padding</item>
<item name="backgroundTint">@color/mtrl_btn_text_btn_bg_color_selector</item>
<item name="rippleColor">@color/mtrl_btn_text_btn_ripple_color</item>
</style>
现在,让我们看看“Widget.MaterialComponents.Button.UnelevatedButton”:
<style name="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:stateListAnimator" ns1:ignore="NewApi">@animator/mtrl_btn_unelevated_state_list_anim</item>
<item name="elevation">0dp</item>
</style>
层次结构到此结束。我没有看到 insetLeft
属性在任何地方被引用,所以你不能在你的布局中覆盖它 - 至少通过你的布局中的 style
引用。
说明您正在尝试做什么以获得可能有帮助的答案。