先放一个最终效果
在设计登录界面时,我想把Button控件的背景设为透明,但是我发现在为Button控件的background设置背景颜色都是无效的。从网上找了各种方法,总结如下:
方法1
完全没什么变化。
android:background="@null"
方法2
设置透明度,背景颜色透明度为0总可以了吧
android:alpha=0
但是这样的话,控件上面的文字也看不到了...
方法3
找到res/values/themes/themes.xml
把两个themes.xml中的
<style name="Theme.UICustomViews" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
都改为
<style name="Theme.UICustomViews" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
这时,我们就可以更改Button控件背景颜色了,可以设button的背景为图片。
我想要透明效果,就可以把Button的背景颜色设置为和底色一模一样的颜色了!(还没有找到合适的设为真正透明的方法)
但是效果图还不理想,Button的边框还没有去掉,增加如下代码后,就是我最终想要的效果图了!
android:outlineProvider="none"
补充方法4
可以自己设计一个Button的背景样式,比如形状和圆角等,把这个xml文件放在drawable下,然后在控件background属性中调用这个就好了
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
android:shape="rectangle">
//shape用于定义形状,有四种形状(矩形rectangle| 椭圆oval | 直线line | 圆形ring)
<solid android:color="#00f"/> //solid用于设置填充形状的颜色
<corners //corners用于创建圆角(只用于形状是矩形)
android:topLeftRadius="8dip"
android:topRightRadius="8dip"
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"/>
</shape>