我正在尝试更改应用程序中所有按钮的边距。我定义了样式:
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="materialButtonStyle">@style/TempButtonStyle</item>
</style>
<style name="TempButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="android:layout_margin">10dp</item>
</style>
</resources>
但是它不起作用。如果我将此样式附加到单个按钮,则效果很好:
<com.google.android.material.button.MaterialButton
android:id="@+id/change_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/change_date"
style="@style/TempButtonStyle"/>
有关如何全局更改的任何建议?
我一直在寻找答案的方法,但答案一直没有。如果要像在问题中一样全局定义Buttonstyle,则至少没有。
问题如下:您可能知道LayoutParams绑定到特定的ViewGroup类型(如LinearLayout或FrameLayout)。这将导致以下问题:您已经以自己的样式定义了layout_margin
,该样式已在vai materialButtonStyle
中全局应用。但是,要应用此属性(和任何布局属性),必须知道View的父级。这就是设置style="@style/TempButtonStyle"
起作用的原因。
为了全局更改“材质按钮”的边距,需要使用以下样式的inset