九江做网站的大公司电商seo引流
一、问题描述
在开发过程中发现安卓的默认主题色是紫色,并且会导致button也是紫色,有时直接在xml布局文件中直接设置button的背景色或者设置背景图片不起效果
方案一、如果是app,可以直接设置主题颜色
比如,将主题设置为白色,
<!-- color.xml中设置颜色-->
<?xml version="1.0" encoding="utf-8"?>
<resources><color name="black">#FF000000</color><color name="white">#FFFFFFFF</color><color name="primaryColor">#FFFFFFFF</color><color name="primaryDarkColor">#FFFFFFFF</color><color name="secondaryColor">#FFFFFFFF</color>
</resources><!-- theme.xml中设置主题颜色,注意Theme.Material3.DayNight.NoActionBar-->
<resources xmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><style name="Base.Theme.MarkCollectionTool" parent="Theme.Material3.DayNight.NoActionBar"><!-- Customize your light theme here. --><!-- <item name="colorPrimary">@color/my_light_primary</item> --><item name="colorPrimary">@color/primaryColor</item><item name="colorPrimaryDark">@color/primaryDarkColor</item><item name="colorAccent">@color/secondaryColor</item></style><style name="Theme.MarkCollectionTool" parent="Base.Theme.MarkCollectionTool" />
</resources>
但是问题是:
1.有可能button从紫色变成白色了,依然不能自己设置颜色
2.如果是自定义library里边,一般为了不与集成的app产生主题冲突,是不设置主题的。
方案二、组件替换
[ 亲测好用 ]
比如设置背景图片的用imageButton,一般的button用“android.wdiget.Button”来代替button
,并且设置button的颜色时尽量通过设置style的方式,而不是直接在布局文件中设置背景色
- imageButton的使用
比如说我需要一个单选按钮radioButton,并且要设置单选按钮的按钮颜色为橘黄色,但是单选框按钮默认为边框黑色的button控件,因为主题的影响选中后还会变成紫色,于是我用一个imagebutton+textview的组合来代替radioButton, imgebutton可以选中前和选中后设置不同的背景图片
//布局文件中通过android:src设置背景图片
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="left"android:layout_marginBottom="10dp"><ImageButtonandroid:id="@+id/selectBtn"android:layout_width="20dp"android:layout_height="20dp"android:layout_marginLeft="25dp"android:src="@drawable/unselectImg"android:onClick="onselectedBtnClicked"android:background="@null"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="我已知道,以后不再提示"android:textSize="13sp"android:textColor="@color/black"/></LinearLayout>//java代码中通过setImageResource设置背景图片
ImageButton selectBtn=(ImageButton) findViewById(R.id.selectBtn);
selectBtn.setImageResource(R.drawable.unselectImg);
2. 使用android.wdiget.Button以及设置样式
首先在res/drawble中新建一个btn_style.xml文件,设置按钮的样式:包括圆角和背景色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:width="345dp" android:height="44dp"><shape android:shape="rectangle"><corners android:radius="22dp" /><solid android:color="#ffffab02" /></shape></item></selector>
然后,在res/style.xml中新建一个按钮的style
<?xml version="1.0" encoding="utf-8"?>
<resources><style name="ButtonStyle" parent="Widget.AppCompat.Button.Colored"><!-- 在这里定义按钮的样式 --><item name="android:textColor">#FF000000</item><item name="android:background">@drawable/btn_style</item><!-- 其他属性 --></style></resources>
最后在布局文件创建button的位置为button设置style
<!--这里用android.widget.Button代替Button, style设置按钮样式-->
<android.widget.Buttonandroid:id="@+id/okBtn"android:layout_width="345dp"android:layout_height="44dp"style="@style/ButtonStyle"android:onClick="onBtnClick"android:text="立即验证"android:layout_marginLeft="20dp"/>
三、补充button的边框样式设置
(以后为了不让主题色影响按钮颜色,推荐使用android.widget.Button代替Button, 二者用法基本一样。 以下遇到按钮都简称button了)
在上述方案二中res/drawle/btn_style.xml设置了按钮的圆角和背景色,这里再补充一下button的边框样式设置方法。
res/drawle下建立一个btn_border.xml文件,输入以下内容设置一个宽度为2dp,颜色为橙黄色的边框,
应用方式:
① 在布局文件中button控件设置:android:background=“@drawable/btn_border”
②跟上述方案二一样,在style.xml中添加一个style,然后在布局文件button组件设置style.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" ><solid android:color="#00000000" /><corners android:radius="21dp" /><stroke android:width="2dp" android:color="#ffab02"/>
</shape>