将 ImageButton 的默认背景设置为透明(使用 AndroidX)
Setting default background of ImageButton to transparent (using AndroidX)
我正在使用 AndroidX 支持库。在我的清单中,我声明:
<application
...
android:theme="@style/AppTheme">
在values.styles.xml
我声明:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:imageButtonStyle">@style/MyImageButton</item>
</style>
<style name="MyImageButton" parent="Widget.AppCompat.ImageButton">
<item name="android:background">@android:color/transparent</item>
</style>
然后我通过以下方式使用 ImageButton:
<ImageButton
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
不幸的是,我的 ImageButton
显示灰色背景而不是透明背景。我该怎么做才能修复它?
您需要替换此行
<item name="android:imageButtonStyle">@style/MyImageButton</item>
和
<item name="imageButtonStyle">@style/MyImageButton</item>
而这也是@
给出的另一种方式
将代码中的这一行添加到
style="@style/MyImageButton"
所以,
<ImageButton
style="@style/MyImageButton"
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
或者您不想在所有按钮标签中添加样式,您可以按照此 answer 更改默认按钮的样式。希望它能帮助你。尝试让我知道它是否有效。
谢谢。
编码愉快:)
您可以简单地将 ImageButton
的 background
设置为 null
,如下所示:
<ImageButton
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@null" />
这将使您的 ImageButton
透明。
我正在使用 AndroidX 支持库。在我的清单中,我声明:
<application
...
android:theme="@style/AppTheme">
在values.styles.xml
我声明:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:imageButtonStyle">@style/MyImageButton</item>
</style>
<style name="MyImageButton" parent="Widget.AppCompat.ImageButton">
<item name="android:background">@android:color/transparent</item>
</style>
然后我通过以下方式使用 ImageButton:
<ImageButton
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
不幸的是,我的 ImageButton
显示灰色背景而不是透明背景。我该怎么做才能修复它?
您需要替换此行
<item name="android:imageButtonStyle">@style/MyImageButton</item>
和
<item name="imageButtonStyle">@style/MyImageButton</item>
而这也是@
将代码中的这一行添加到
style="@style/MyImageButton"
所以,
<ImageButton
style="@style/MyImageButton"
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
或者您不想在所有按钮标签中添加样式,您可以按照此 answer 更改默认按钮的样式。希望它能帮助你。尝试让我知道它是否有效。
谢谢。 编码愉快:)
您可以简单地将 ImageButton
的 background
设置为 null
,如下所示:
<ImageButton
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@null" />
这将使您的 ImageButton
透明。