以编程方式创建具有轮廓样式的 MaterialButton

2024-01-09

我想以编程方式创建一个按照设计指南中定义的按钮:https://material.io/design/components/buttons.html#outlined-button https://material.io/design/components/buttons.html#outlined-button,看起来像这样:

在 XML 中,我可以使用这段布局 xml 来做到这一点:

<com.google.android.material.button.MaterialButton
    android:id="@+id/buttonGetStarted"
    style="@style/Widget.MaterialComponents.Button.OutlinedButton"
    android:text="@string/title_short_intro" />

我正在寻找一个示例,展示如何使用 Java 代码执行此操作?我已经尝试过以下方法:

MaterialButton testSignIn = new MaterialButton( new ContextThemeWrapper( this, R.style.Widget_MaterialComponents_Button_OutlinedButton));
String buttonText = "Sign-in & empty test account";
testSignIn.setText( buttonText );

但这不会导致轮廓变体:


您可以使用以下内容:

MaterialButton testSignIn = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
String buttonText = "Sign-in & empty test account";
testSignIn.setText(buttonText);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以编程方式创建具有轮廓样式的 MaterialButton 的相关文章

随机推荐