MaterialButton 和简单的 Button 之间有什么区别吗? [英] Is any difference between a MaterialButton and a simple Button?
问题描述
如果我将Theme.MaterialComponents.Light设置为我的主题,如果我在xml布局中使用这两个按钮会有什么区别吗?
If I set Theme.MaterialComponents.Light as my main theme, will be any difference between these two buttons if I use them in an xml layout?
<Button />
<com.google.android.material.button.MaterialButton />
正如我所见,它们的行为都与 MaterialButton 相同.如果我想获得旧的普通按钮的行为,我必须使用:
As I saw they behave both as MaterialButtons. If I want to get the behavior of the old plain button, I have to use:
<androidx.appcompat.widget.AppCompatButton />
提前致谢!
推荐答案
如果您使用的是 MaterialComponents 主题, 和
If you are using a MaterialComponents Theme there is no difference between <Button />
and <com.google.android.material.button.MaterialButton />
.
启用了自动膨胀,将用<com.google.android.material替换
在运行时.
There is an auto-inflation enabled which will replace <Button
with <com.google.android.material.button.MaterialButton
at runtime.
MaterialComponentsViewInflater
在膨胀时用 Material Components 替换一些框架小部件,前提是使用 MaterialComponents 主题.
AppCompat 也会发生类似的情况(您可以检查 MaterialComponentsViewInflater 扩展 AppCompatViewInflater
).
这意味着,<Button
在运行时被替换为 <com.google.android.material.button.MaterialButton
,如果您使用的是 MaterialComponents 主题.
It means that, the <Button
is replaced <com.google.android.material.button.MaterialButton
at runtime, if you are using a MaterialComponents Theme.
如果我想获得旧的普通按钮的行为,我必须使用:
.
If I want to get the behavior of the old plain button, I have to use:
<androidx.appcompat.widget.AppCompatButton />
.
这是一种选择,但不一定.
这取决于您想要实现的目标.您还可以配置自定义样式,因为 Widget.MaterialComponents.Button
样式由 Widget.AppCompat.Button
样式继承.
It is an option, but not necessarily.
It depends by what you want to achieve. You can also config a custom style since the Widget.MaterialComponents.Button
style inherits by Widget.AppCompat.Button
style.
就是区别:
<style name="Widget.MaterialComponents.Button" parent="Widget.AppCompat.Button">
<item name="enforceMaterialTheme">true</item>
<item name="enforceTextAppearance">true</item>
<item name="android:textAppearance">?attr/textAppearanceButton</item>
<item name="android:textColor">@color/mtrl_btn_text_color_selector</item>
<item name="android:paddingLeft">@dimen/mtrl_btn_padding_left</item>
<item name="android:paddingRight">@dimen/mtrl_btn_padding_right</item>
<item name="android:paddingTop">@dimen/mtrl_btn_padding_top</item>
<item name="android:paddingBottom">@dimen/mtrl_btn_padding_bottom</item>
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
<item name="android:insetTop">@dimen/mtrl_btn_inset</item>
<item name="android:insetBottom">@dimen/mtrl_btn_inset</item>
<item name="android:stateListAnimator" ns2:ignore="NewApi">@animator/mtrl_btn_state_list_anim</item>
<item name="cornerRadius">@null</item>
<item name="elevation">@dimen/mtrl_btn_elevation</item>
<item name="iconPadding">@dimen/mtrl_btn_icon_padding</item>
<item name="iconTint">@color/mtrl_btn_text_color_selector</item>
<item name="rippleColor">@color/mtrl_btn_ripple_color</item>
<item name="backgroundTint">@color/mtrl_btn_bg_color_selector</item>
<item name="shapeAppearance">?attr/shapeAppearanceSmallComponent</item>
</style>
这篇关于MaterialButton 和简单的 Button 之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!