我想以编程方式创建一个按钮,如此处的设计准则所定义: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 );
但这不会导致大纲变体:
答案 0 :(得分:1)
您可以在下面使用:
MaterialButton testSignIn = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
String buttonText = "Sign-in & empty test account";
testSignIn.setText(buttonText);
答案 1 :(得分:0)
MaterialButton
具有strokeColor
和strokeWidth
,用于设置轮廓。
val _strokeColor = getColorStateList(R.styleable.xxx_strokeColor)
val _strokeWidth = getDimensionPixelSize(R.styleable.xxx_strokeWidth, 0)
button = MaterialButton(context).apply {
layoutParams = LayoutParams(MATCH_PARENT, WRAP_PARENT)
strokeColor = _strokeColor
strokeWidth = _strokeWidth
}
答案 2 :(得分:0)
创建概述的按钮布局outlined_button.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.material.button.MaterialButton>
然后在运行时膨胀概述的按钮
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MaterialButton button = (MaterialButton)inflater.inflate(R.layout.outlined_button, vg, false);
答案 3 :(得分:0)