This is my code:
<android.support.design.widget.FloatingActionButton
android:id="@+id/enter_floating_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="40dip"
android:layout_marginRight="10dip"
android:clickable="true"
app:background="#00FF00"
app:backgroundTint="#00FF00"
app:rippleColor="@android:color/red"
app:fabSize="normal"
android:src="@drawable/ic_done" />
As you can see, I set app:background
and app:backgroundTint
, but these are useless, FloatingActionButton's
background color doesn't change, and either the app:rippleColor
, when i press the button it's color isn't which I set, looks like the accentColor in the theme.
Why don't these attributes work?
How can I change the FloatingActionButton's
background and ripple color?
I had seen this: Android changing Floating Action Button color . Some answers may work in 22, but I didn't find a useful way in 23.
浮动动作按钮将从styles.xml中的colorAccent属性中获取颜色。设置颜色并为该浮动动作按钮设置该样式,然后它将起作用
Try to add programmatically ripple color in your FloatingActionButton class:
public RippleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.RippleView);
mRippleColor = a.getColor(R.styleable.RippleView_rippleColor,
mRippleColor);
mAlphaFactor = a.getFloat(R.styleable.RippleView_alphaFactor,
mAlphaFactor);
mHover = a.getBoolean(R.styleable.RippleView_hover, mHover);
a.recycle();
}
public void init() {
mDensity = getContext().getResources().getDisplayMetrics().density;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAlpha(20);
setRippleColor(Color.BLACK, 0.2f);
// setRippleColor(Color.parseColor(getResources().getColor(R.color.greycolor), 0.2f);
// setRippleColor(0x000000, 0.1f);
}
public void setRippleColor(int rippleColor, float alphaFactor) {
mRippleColor = rippleColor;
mAlphaFactor = alphaFactor;
}
public void setHover(boolean enabled) {
mHover = enabled;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.