use*_*644 23 java android android-layout floating-action-button
我想知道如何更改支持库22.2.0中的浮动操作按钮颜色?我试过了
button.setBackgroundColor(color);
Run Code Online (Sandbox Code Playgroud)
但显然,这会改变按钮的可绘制性并变为正方形.
现在我想知道如何改变颜色而不仅仅是颜色,而不会触及形状?
提前致谢
Olu*_*ith 34
也许迟到但可以提供帮助.
fab.setBackgroundTintList(ColorStateList.valueOf(Color
.parseColor("#33691E")));
Run Code Online (Sandbox Code Playgroud)
并从您可以在此处找到的颜色列表中解析实际颜色代码
tac*_*lux 31
创建一个ColorStateList
并将其设置为背景色调:
button.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{color}));
Run Code Online (Sandbox Code Playgroud)
Ada*_*hns 10
在colors.xml
(R.color.purple
在这种情况下)创建一个颜色资源并像这样使用它:
floatingActionButton.setBackgroundTintList(getResources().getColorStateList(R.color.purple));
Run Code Online (Sandbox Code Playgroud)
为此向后兼容:
DrawableCompat.setTintList(DrawableCompat.wrap(fab.getDrawable()), tintColor); // <- icon
DrawableCompat.setTintList(DrawableCompat.wrap(fab.getBackground()), backgroundTintColor); // <- background
Run Code Online (Sandbox Code Playgroud)
小智 9
方法1:更改xml中的浮动操作栏(fab)颜色:
要更改浮动操作栏(fab)颜色,请按照此步骤操作
只需在xml的浮动操作栏(fab)中添加"app:backgroundTint ="#colorcode"".例如
app:backgroundTint="#8393ca"
Run Code Online (Sandbox Code Playgroud)
在#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" />
Run Code Online (Sandbox Code Playgroud)
方法2:以编程方式更改浮动操作栏颜色
只需在代码中添加此行即可
首先在values => colors中创建一个红色,然后在create中的活动中添加此代码
fab.setBackgroundTintList(getResources().getColorStateList(R.color.red));
or
fab.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#33691E")));
Run Code Online (Sandbox Code Playgroud)
学分:http://androidrace.com/2016/12/12/how-to-change-fabfloating-action-bar-color-android/