【发布时间】:2019-02-06 18:21:05
【问题描述】:
我想以编程方式创建一个此处设计指南中定义的按钮: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 );
但这不会导致大纲变体:
【问题讨论】:
-
如果我没记错的话,那就是:
style="@style/Widget.AppCompat.Button.Borderless.Colored看看这是否能回答你的问题。 -
刚刚对此进行了测试(将我的代码中的 R.style.Widget_MaterialComponents_Button_OutlinedButton 更改为 R.style.Widget_AppCompat_Button_Borderless_Colored)。这没什么区别。
-
将
style="@style/Widget.AppCompat.Button.Borderless.Colored添加到 xml 并在 java-kotlin 中相同,然后尝试。 -
你有想过这个吗?
-
@masterwork,不...仍然使用基于 XML 的布局作为解决方法(如果您找到解决方案,请随时更新)。
标签: android android-layout material-design android-button androidx