【问题标题】:How to create borderless button using style from style.xml file (along with all other styles)如何使用 style.xml 文件中的样式(以及所有其他样式)创建无边框按钮
【发布时间】:2018-02-16 09:08:23
【问题描述】:

我正在尝试创建一个无边框按钮,但我的按钮还有许多其他样式,我想通过将代码嵌入到我的 style.xml 文件中来设计无边框按钮。

我发现的一种方法是: 通过在我的布局文件中使用 style="?android:attr/borderlessButtonStyle"。

<Button
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"
    style="?android:attr/borderlessButtonStyle" />

但我想在 style.xml 中实现这一点,但我不知道 ' _______ 采用什么值?

【问题讨论】:

标签: android xml android-layout android-button


【解决方案1】:

试试这个hereanother

android:background="?android:attr/selectableItemBackground"

【讨论】:

  • 您应该使用 cmets 要求澄清。
  • @RandallArms 好吧,我以为我刚刚发布了答案。我尝试发表评论,但它需要 50 声望才能发表评论希望你知道。但在未来我会记住它。所以我重新编辑了答案,现在它看起来不像我要求澄清。谢谢!
  • 现在看起来不错!希望我没有表现得太迂腐。
  • @RandallArms 可以在很小的时候停止犯错。所以谢谢你。
【解决方案2】:

您可以通过制作扩展默认按钮类的自定义按钮类来做到这一点,并在任何地方使用这个类,第二种可能的解决方案是使用

如果它被选中或未被选中,你应该使用切换按钮https://developer.android.com/reference/android/widget/ToggleButton.html

请注意,仍然有 4 个状态

你在这样的选择器中定义它们

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" />
    <item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/>
    <item android:state_checked="true" android:drawable="@drawable/likeon"/>
    <item android:drawable="@drawable/likeinitial"/>
</selector>

然后像这样在你的按钮中定义它

android:background="@drawable/like_button"

【讨论】:

    【解决方案3】:

    我想这就是你要找的。​​p>

    <style name="style_name" parent="@style/Widget.AppCompat.Button.Borderless">
        <!-- your style -->
    </style>
    

    【讨论】:

    • 欢迎来到 Stack Overflow。虽然这段代码可能会回答这个问题,但提供关于这段代码为什么和/或如何回答这个问题的额外上下文可以提高其长期价值。How to Answer
    猜你喜欢
    • 2014-04-10
    • 2017-10-21
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    相关资源
    最近更新 更多