![](/img/trans.png)
[英]change FloatingActionButton and SubActionButton background color
[英]Programmatically change background Color of FloatingActionButton
我尝试了setBackgroundTintList方法,但它需要min api 21,这不是大多数程序员想要的。 我想更改FloatingActionButton的bgcolor,但通过编程方式保持向后兼容性。 有什么办法或解决方法吗?
你可以用
mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
例如:
fabAdd.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(mContext, R.color.main_screen_status_bar)));
如果您在构建中使用支持库:
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:preference-v7:23.2.1'
compile 'com.android.support:preference-v14:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-vector-drawable:23.2.1'
compile 'com.android.support:animated-vector-drawable:23.2.1'
并在您的班级导入此:
import android.support.design.widget.FloatingActionButton;
您应该可以致电:
setBackgroundTintList
您还需要在布局文件中使用Widget的支持版本。
我认为您也可以使用DrawableCompat类,但是我对此没有任何经验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.