如何以材料或设计材料编程方式设置材料按钮样式
根据新材料设计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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,我找到了一种解决方法。
它并不像应有的那样简单,但是可以正常工作。
您可以做的是:
我希望这会有所帮助!
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:
I hope this helps!