Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式从Appcompat 22.2.0更改浮动操作按钮的颜色_Java_Android_Android Layout_Floating Action Button - Fatal编程技术网

Java 以编程方式从Appcompat 22.2.0更改浮动操作按钮的颜色

Java 以编程方式从Appcompat 22.2.0更改浮动操作按钮的颜色,java,android,android-layout,floating-action-button,Java,Android,Android Layout,Floating Action Button,我想知道如何从支持库22.2.0更改浮动操作按钮的颜色? 我试过了 button.setBackgroundColor(color); 但很明显,这改变了按钮的可绘制性,它变成了正方形 现在我想知道如何改变颜色,但只是颜色,而不触及形状 提前感谢属性名称为backgroundTint 所以我认为有一个函数叫做 按钮。设置背景色调(颜色)创建一个颜色状态列表,并将其设置为背景色调: button.setBackgroundTintList(new ColorStateList(new int[]

我想知道如何从支持库22.2.0更改浮动操作按钮的颜色? 我试过了

button.setBackgroundColor(color);
但很明显,这改变了按钮的可绘制性,它变成了正方形

现在我想知道如何改变颜色,但只是颜色,而不触及形状


提前感谢

属性名称为
backgroundTint

所以我认为有一个函数叫做


按钮。设置背景色调(颜色)
创建一个
颜色状态列表
,并将其设置为背景色调:

button.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{color}));

colors.xml
中创建颜色资源(本例中为
R.color.purple
),并按如下方式使用:

floatingActionButton.setBackgroundTintList(getResources().getColorStateList(R.color.purple));

请在此处检查已接受的答案:

如果你想改变颜色

  • 在属性为app:backgroundTint的XML中
  • 在代码中使用.setbackgroundtinlist
你必须使用

  • 在带有
    属性app:backgroundTint的XML中
  • 在代码中使用.setBackgroundTintList 读这个答案

如果从开始使用浮动操作按钮库,请使用此

fab.setColorNormal(getResources().getColor(R.color.fab_color1));

也许晚了,但可能会有帮助

 fab.setBackgroundTintList(ColorStateList.valueOf(Color
                    .parseColor("#33691E")));

并从颜色列表中解析实际颜色代码

,以实现向后兼容:

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); // <- icon
DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor); // <- background

DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()),tintColor);// 方法1:更改xml中的浮动操作栏(fab)颜色:

要更改浮动操作栏(fab)颜色,只需执行以下步骤

只需在浮动操作栏(fab)的xml中添加“app:backgroundTint=“#colorcode”。。比如说

app:backgroundTint="#8393ca"
在#8393ca处添加您想要的任何颜色代码

例如:用法

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="#8393ca"
    android:src="@drawable/send" />

信用证:

只需在xml文件的浮动操作按钮下使用这一行即可

android:backgroundTint="#96989A"

试试这个代码。它将为背景资源添加色调

button.setBackgroundTintList(getResources().getColorStateList(R.color.yourColor));

XML背景:着色颜色将始终覆盖您提供的任何颜色 以编程方式。因此,要以编程方式提供颜色,请从XML文件中删除
android:backgroundTint=“#96989A”
行,并使用:

button.setBackgroundTintList(getResources().getColorStateList(R.color.yourColor));

有时你在颜色文件中得到了你的颜色,你想用那个

您可以执行以下代码

fab.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(getString(R.color.youcolor))));
如您所知,要以编程方式更改它,您需要使用
setBackgroundTintList()
,然后传递所需的
ColorStateList.valueOf()
,此函数获取一个certein int并解析它

注意:如果您立即使用int颜色,有时可能会给您带来获取所需颜色的问题,因此我不使用它,也不建议您这样使用它

然后放入
ColorStateList.valueOf()
Color.parseColor()
这就是您需要放入的内容

这需要一个字符串,但是颜色文件中有int颜色,那么怎么办?


Color.parseColor()
pass
getString()
中,这是每个活动都有的方法,因此在
getString()
中,您最后将您的颜色设置为this
R.Color。您的颜色将动态设置,因此不可能有xml资源文件动态创建颜色状态列表?基于这个问题,假设
按钮
是一个。这意味着它将被限制在v7.或简单地
ColorStateList.valueOf(color)
,from:注意:如果以编程方式设置背景色,请不要在XML中设置任何与背景色相关的属性。刚刚发现了XML默认值总是会超过代码中任何运行时更改的困难方式(奇怪)。请考虑解释性文本以配合您的代码,以帮助将来的读者。谢谢。这对我很有帮助。只是想补充一点:如果您不想使用硬编码字符串颜色值,而是引用存储在colors.xml中的颜色,您可以使用以下内容:
fab.setbackgroundtinlist(ColorStateList.valueOf(ContextCompat.getColor(getActivity(),R.color.white))
这是仅来自API级别21的名称。@marienke您确定在API 21下测试时使用了xml中的
应用程序
名称空间吗?@LouisCAD您自找的:)@marienke您只想在xml中使用
ColorStateList
,请使用
设置启用(isnfceEnabled)
from code,使其使用
ColorStateList
xml文件中的正确颜色。@marienke然后,查看
CheckedTextView
源代码,使您的FAB子类具有选中的
状态,您将在
ColorStateList
中使用该状态。然后,当NFC打开或关闭时,您只需通过编程方式更改晶圆厂的选中状态,可能需要使用
setChecked(…)
方法。这不是“编程方式”,而是将文本颜色更改为相同的背景色。如何避免它或更改文本颜色,如#FFF?我注意到,如果您使用的是可绘制图标,这也会将您删除。
fab.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(getString(R.color.youcolor))));