如何以材料或设计材料编程方式设置材料按钮样式

发布于 2025-02-11 08:58:55 字数 1206 浏览 0 评论 0原文

根据新材料设计3 ,您可以设置不同的按钮样式喜欢:

在XML布局中使用以下代码:

<com.google.android.material.button.MaterialButton
            style="@style/Widget.Material3.Button.ElevatedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Elevated" />

        <com.google.android.material.button.MaterialButton
            style="@style/Widget.Material3.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Filled" />

我想要的是,我想在用户首次按下按钮时应用此填充的按钮。因此,当用户单击高架按钮时,它应该变成填充样式按钮。意思是,我想在单击“高架”按钮的“侦听器”中以Java代码进行编程应用样式。所以有人可以帮我吗?

我对此进行了很多搜索,但是我发现的是,我们可以在用Java代码编程并设置样式的同时应用样式。但是我想在运行时只设置样式,而不是整个按钮本身,否则我必须创建按钮并在Java代码中执行添加/删除按钮。

任何帮助将不胜感激。

As per new material design 3, you can set the different button styles like:

enter image description here

Using below code in xml layout:

<com.google.android.material.button.MaterialButton
            style="@style/Widget.Material3.Button.ElevatedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Elevated" />

        <com.google.android.material.button.MaterialButton
            style="@style/Widget.Material3.Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Filled" />

What I want is, I want to apply this Filled style of button when user press on button first time. So when user click on Elevated button, it should turn into Filled style button. Means, I want to apply the style programmatically from Java code in click listener of Elevated button. So anyone can help me please ?

I search much on that but what I found is, we can apply the style while creating Button itself programmatically in Java code and set the style. But I want to set only style at run time not entire button itself, otherwise I have to create button and perform add/remove button in java code.

Any help would be much more appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-02-18 08:58:55

我遇到了同样的问题,我找到了一种解决方法。
它并不像应有的那样简单,但是可以正常工作。

您可以做的是:

  1. 首先,为按钮创建一个自定义属性:
<attr name="myButtonStyle" format="reference" />
  1. 接下来,定义要使用的样式,例如:
<item name="myButtonTextStyle">@style/Widget.Material3.Button.ElevatedButton</item>
  1. 最后,您可以使用这些样式以编程方式创建按钮:
val button = MaterialButton(context, null, R.attr.myButtonTextStyle)
button.text = "My Button"

我希望这会有所帮助!

I encountered the same issue and I found a way to solve it.
It's not as simple as it should be, but it works.

Here's what you can do:

  1. First, create a custom attribute for your button:
<attr name="myButtonStyle" format="reference" />
  1. Next, define the style you want to use, for example:
<item name="myButtonTextStyle">@style/Widget.Material3.Button.ElevatedButton</item>
  1. Finally, you can use these to create the button programmatically:
val button = MaterialButton(context, null, R.attr.myButtonTextStyle)
button.text = "My Button"

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文