【问题标题】:Programmatically create a MaterialButton with Outline style以编程方式创建具有大纲样式的 MaterialButton
【发布时间】: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


【解决方案1】:

你可以在下面使用:

MaterialButton testSignIn = new MaterialButton(context, null, R.attr.borderlessButtonStyle);
String buttonText = "Sign-in & empty test account";
testSignIn.setText(buttonText);

【讨论】:

  • R.attr.borderlessButtonStyle 不会创建轮廓按钮。我认为正确的属性是R.attr.materialButtonOutlinedStyle
  • 谢谢,我使用的是普通类Button,因为他们声称他们可以转换它们,但是当它通过代码完成时,我不会神奇地转换它们,只能从 xml。
【解决方案2】:

如果你想应用 Outlined 按钮,你可以在构造函数中使用 R.attr.materialButtonOutlinedStyle 属性样式:

MaterialButton outlinedButton = new MaterialButton(context,null, R.attr.materialButtonOutlinedStyle);
outlinedButton.setText("....");

【讨论】:

  • 完美运行,这里适用于 Xamarin.Android:new MaterialButton(Context, null, Resource.Attribute.materialButtonOutlinedStyle);
【解决方案3】:

MaterialButtonstrokeColorstrokeWidth 用于设置轮廓。

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
}

【讨论】:

    【解决方案4】:

    创建轮廓按钮布局 outline_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);
    

    【讨论】:

    • 这对我有用。 Kotlin 膨胀代码: val b: MaterialButton = this.layoutInflater.inflate(R.layout.outline_button, null) as MaterialButton
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2021-10-04
    • 2020-06-20
    • 2011-02-26
    • 2020-10-04
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多