【问题标题】:Button background as transparent按钮背景为透明
【发布时间】:2011-02-10 06:36:11
【问题描述】:

我有一个按钮。当我按下按钮时,我必须将文本设置为粗体,否则正常。所以我写了粗体和正常的样式。

<style name="textbold" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textregular" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">normal</item>
</style>

现在我有一个 button_states.xml 为:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
    style="@style/textbold" />
<item android:state_focused="false" android:state_pressed="true"
    style="@style/textregular" />

<item style="@style/textregular" />
</selector> 

在此按钮的布局中,我还必须将背景设置为透明...我该怎么做?我的布局代码是:

<Button android:id="@+id/Btn" android:background="@drawable/button_states" />

如何在我的样式中包含透明背景?

【问题讨论】:

  • 这么多好的答案现在可以被接受了...
  • 查看this answer here 制作带边框的透明按钮
  • ' 你为什么不直接使用 TextView 并在它的 onClick 上调用操作呢?

标签: android


【解决方案1】:

要使背景透明,只需 android:background="@android:color/transparent"

但是,您的问题似乎有点深,因为您以一种非常奇怪的方式使用选择器。您使用它的方式似乎是错误的,但如果它确实有效,您应该将背景图像作为&lt;item/&gt; 放在样式中。

仔细查看 Android 源代码中如何使用样式。虽然它们不会在单击按钮时更改文本样式,但有很多关于如何实现目标的好主意。

【讨论】:

  • 看看我的澄清答案。我误读了你的问题开始。背景设置为样式。
  • 对不起,我不清楚解决方案。只设置@android:color/transparent 不支持普通按钮的on-touch 样式(相当于将背景设置为@null)。
  • @gatoatigrado:你的意思是你想让一个透明区域用橙色勾勒出轮廓或用一些什么阴影?如果是这样,您需要为此创建自定义的 9-patch 图像。
  • @StevePomeroy,是的,Android 4.1 上的默认按钮效果是 [on button press] 背景为浅蓝色,带有更浅的蓝色轮廓。
  • @gatoatigrado:好的。如果你想要那样,那么你不能使用透明按钮。我不确定你想在那里完成什么。
【解决方案2】:

尝试设置背景透明的新方法

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

【讨论】:

  • 这是我推荐使用的。这样,您就可以为按钮保留漂亮的材料设计动画 =)
  • 这是更好的答案
  • 如果有人想要一个圆形的行为,使用:`android:background="?android:attr/selectableItemBackgroundBorderless"`
【解决方案3】:

您还可以使用: 在你的 xml 中:

android:background="@null"

或在代码中:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

【讨论】:

  • 支持提供替代方案,喜欢看到所有选项!
【解决方案4】:

使用#0000(只有四个零,否则将被视为black)这是透明的颜色代码。您可以直接使用它,但我建议您在 color.xml 中定义一种颜色,这样您就可以享受代码的重用性。

【讨论】:

  • 幸运的是,这已经为我们定义为@android:color/transparent
【解决方案5】:

将此添加到您的 Xml - android:background="@android:color/transparent"

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"
            android:background="@android:color/transparent"
            android:textStyle="bold"/>

【讨论】:

    【解决方案6】:

    我用 XML 实现了这一点

    android:background="@android:color/transparent"
    

    【讨论】:

      【解决方案7】:

      我用过

      btn.setBackgroundColor(Color.TRANSPARENT);
      

      android:background="@android:color/transparent"
      

      【讨论】:

      • btn.setBackgroundColor(Color.TRANSPARENT);适合我
      【解决方案8】:

      选择器仅适用于可绘制对象,不适用于样式。 Reference


      首先,要使按钮背景透明,请使用以下属性,因为这不会影响材料设计动画:

      style="?attr/buttonBarButtonStyle"
      

      有很多方法可以为您的按钮设置样式。查看this tutorial

      第二,要在按下时使文本变为粗体,请使用以下 java 代码:

      btn.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
      
              switch (event.getAction()) {
                  // When the user clicks the Button
                  case MotionEvent.ACTION_DOWN:
                      btn.setTypeface(Typeface.DEFAULT_BOLD);
                      break;
      
                  // When the user releases the Button
                  case MotionEvent.ACTION_UP:
                      btn.setTypeface(Typeface.DEFAULT);
                      break;
              }
              return false;
          }
      });
      

      【讨论】:

        【解决方案9】:

        我们可以在 Button xml 中使用 android:background 属性,如下所示。

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

        或者我们可以使用style

        style="?android:attr/borderlessButtonStyle" 用于透明和无阴影背景。

        【讨论】:

          【解决方案10】:

          您可以通过设置颜色 alpha 通道来实现。

          配色方案是这样的#AARRGGBB,其中 A 代表 alpha 通道(透明度),R 代表红色,G 代表绿色,B 代表蓝色。

          【讨论】:

            【解决方案11】:

            第 1 步: 在drawable中创建一个新的资源文件并复制粘贴

            <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android">
            
                <stroke android:color="#fff" android:width="2dp"/>
                <corners android:radius="25dp"/>
                <padding android:right="15dp" android:top="15dp" android:bottom="15dp" android:left="15dp"/>
            </shape>
            

            将其保存为 ButtonUI(假设)

            第 2 步:将 UI 应用到按钮 xml

            <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="join the crew"
              android:background="@drawable/ButtonUI"
              android:textColor="#fff"/>
            

            【讨论】:

              【解决方案12】:

              代码:

              button.setVisibility(View.INVISIBLE);
              

              XML:

              android:background="@android:color/transparent"
              

              【讨论】:

                【解决方案13】:

                我会说扩展 Borderless 样式。

                <style name="Button" parent="Widget.AppCompat.Button.Borderless">
                    <item name="android:textColor">@color/blue</item>
                    <item name="android:textAllCaps">true</item>
                </style>
                

                【讨论】:

                  【解决方案14】:

                  您可以通过在 xml 文件中添加以下属性来轻松完成此操作。这段代码经过大量时间的测试。

                  android:background="@android:color/transparent"
                  

                  【讨论】:

                  • 为什么要添加这个答案?此解决方案已在其他两个答案中,包括得票最多的答案。
                  【解决方案15】:

                  单击按钮时,您将背景颜色应用为transparent(light gray)

                  ButtonName.setOnClickListener()
                  

                  在上面的方法中你设置了按钮的背景颜色。

                  【讨论】:

                  • 担心这被赞成了。请不要这样做。未来的维护者会诅咒你的名字。使用选择器设置背景,并有一种颜色/可绘制用于按下,一种用于不按下。不要编写代码在侦听器中更改它 - 这不是这样做的。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-05-20
                  • 2015-02-22
                  • 1970-01-01
                  • 2014-06-20
                  • 1970-01-01
                  • 2014-12-19
                  • 2011-05-19
                  相关资源
                  最近更新 更多