【问题标题】:How to remove button shadow (android)如何删除按钮阴影(android)
【发布时间】:2015-04-29 15:09:46
【问题描述】:

我想去掉按钮的阴影,让它看起来更平坦。

我现在有这个:

但我想要这个:

【问题讨论】:

标签: android button shadow


【解决方案1】:

将其用作按钮的背景可能会有所帮助,根据需要更改颜色

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <solid android:color="@color/app_theme_light" />
            <padding
                android:left="8dp"
                android:top="4dp"
                android:right="8dp"
                android:bottom="4dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/app_theme_dark" />
            <padding
                android:left="8dp"
                android:top="4dp"
                android:right="8dp"
                android:bottom="4dp" />
        </shape>
    </item>
</selector>

【讨论】:

  • 您能解释一下这是如何工作的吗?它没有为我移除阴影。
  • 它将原始背景替换为阴影,此背景为纯色
【解决方案2】:

另一种选择是添加

style="?android:attr/borderlessButtonStyle"

到您的 Button xml,如此处所述 http://developer.android.com/guide/topics/ui/controls/button.html

一个例子是

<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" />

【讨论】:

  • 这会删除按钮样式,例如阴影和浮雕。添加带有形状的选择器是可选的。这是一个更准确的答案。
  • 另外,我为我的按钮使用了自定义样式。您可以扩展您的自定义样式:
【解决方案3】:

我使用自定义样式

<style name="MyButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless"></style>

别忘了添加

<item name="android:textAllCaps">false</item>

否则按钮文本将大写。

【讨论】:

    【解决方案4】:

    试试:android:stateListAnimator="@null"

    【讨论】:

    • 虽然这可能会解决 OP 的问题,但我建议为其添加一些上下文。为什么会有帮助?此外,“试试这个”有点误导。你确定它会解决问题,还是只是猜测?如果是这样,你应该写一个评论。
    • 它需要 API 21。
    【解决方案5】:

    一种更简单的方法是将此标签添加到您的按钮:

    android:stateListAnimator="@null"
    

    虽然它需要 API 级别 21 或更高..

    【讨论】:

    • 对我很有帮助
    • 我更喜欢这种方法,它更灵活。
    • @Alon Kogan,使用 android:stateListAnimator 属性是否有任何向后兼容性?
    • 此解决方案允许您创建一个必须继承自 borderlessButtonStyle 样式的样式机器人,从而为您提供更大的灵活性。
    • 这只是对我有用的解决方案,borderlessButtonStyle 标签不起作用,因为我需要在按下按钮时摆脱阴影
    【解决方案6】:

    @Alt-Cat 答案对我有用!

    R.attr.borderlessButtonStyle 不包含阴影。

    而且按钮的文档很棒。

    此外,您可以在自定义按钮上设置此样式,在第二个构造函数中。

        public CustomButton(Context context, AttributeSet attrs) {
            this(context, attrs, R.attr.borderlessButtonStyle);
        }
    

    【讨论】:

      【解决方案7】:

      科特林

      stateListAnimator = null
      

      Java

      setStateListAnimator(null);
      

      XML

      android:stateListAnimator="@null"
      

      【讨论】:

      • 简单干净。
      • 需要 API 级别 21 或更高
      • 在将 stateListAnimator 设置为 null 按钮后变得不可见,有人遇到这种情况吗?
      • 这不仅可以消除阴影。
      【解决方案8】:

      您可以使用 TextView 代替 Button,并在 java 代码中添加点击侦听器。

      在活动布局xml中:

      <TextView
          android:id="@+id/btn_text_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/colorPrimaryDark"
          android:text="@string/btn_text"
          android:gravity="center"
          android:textColor="@color/colorAccent"
          android:fontFamily="sans-serif-medium"
          android:textAllCaps="true" />
      

      在活动java文件中:

      TextView btnTextView = (TextView) findViewById(R.id.btn_text_view);
      btnTextView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      // handler code
                  }
              });
      

      【讨论】:

      • 这不是正确答案,但您实际上帮助我解决了类似问题,所以谢谢!
      • 啊哈!好主意——让我快速解决问题——
      【解决方案9】:

      材料设计按钮添加到按钮 xml: style="@style/Widget.MaterialComponents.Button.UnelevatedButton"

      【讨论】:

      【解决方案10】:

      以上所有答案都很好,但我会建议另一种选择:

      <style name="FlatButtonStyle" parent="Base.Widget.AppCompat.Button">
       <item name="android:stateListAnimator">@null</item>
       <!-- more style custom here --> 
      </style>
      
      
      

      【讨论】:

      【解决方案11】:

      带有自定义按钮;使用样式R.style.Widget_AppCompat_Button_Borderless,Kotlin 方式-

      class CSButton @JvmOverloads constructor(
          context: Context,
          attrs: AttributeSet? = null,
          defStyleAttr: Int = R.style.Widget_AppCompat_Button_Borderless
      ) : AppCompatButton(context, attrs, defStyleAttr)
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 1970-01-01
        • 2015-03-08
        • 2017-12-05
        • 1970-01-01
        • 2016-11-23
        • 1970-01-01
        • 2022-01-01
        • 2015-04-28
        相关资源
        最近更新 更多