关于购物按钮视图徽章的问题。我想在购物按钮上添加徽章。我读到的所有代码都是关于将按钮及其通知添加到菜单和操作栏中,我不想将按钮添加到菜单中。该应用程序是我通过Navigation Drawer活动创建的。此活动会自动创建一个就绪按钮,为您提供所需的任何内容。这个代码用于我添加到它的按钮和徽章,但我想要这些徽章的代码,但在操作栏之外
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contextClickable="true"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_add_shopping_cart_white_48dp"
app:useCompatPadding="true"
tools:targetApi="m" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="48dp"
android:layout_gravity="bottom|right"
tools:ignore="RtlHardcoded">
<TextView
android:id="@+id/badge_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:gravity="center"
android:textColor="#FFF"
android:textSize="11sp"
android:background="@drawable/badge_circle"/>
</RelativeLayout>
和这个命令代码按钮&#34; fab&#34;在主要活动中
private static final int ACTIVITY_REQUEST_CODE = 6 ;
private static final int ACTIVITY_REQUEST_CODE2 = 3 ;
private static final int ACTIVITY_REQUEST_CODE3 = 4 ;
private static final int ACTIVITY_REQUEST_CODE4 = 5 ;
String sandwish;
int sandwishI;
String snack;
int snackI;
String juice;
int juiceI;
String pizza;
int pizzaI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
final FloatingActionButton fab = (FloatingActionButton) findViewById( R.id.fab );
fab.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent( MainActivity.this, Order.class );
Bundle b = new Bundle();
b.putString("string", sandwish);
b.putString("name",snack );
b.putString("value1", juice);
b.putString("value2",pizza );
b.putInt( "INT",sandwishI );
b.putInt( "INT1",snackI );
b.putInt( "INT2",juiceI );
b.putInt( "INT3",pizzaI );
i.putExtra("personBdl", b);
startActivity( i );
}
} );
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((resultCode == RESULT_OK) && (requestCode == ACTIVITY_REQUEST_CODE)) {
sandwish = data.getExtras().getString( Sandwish.REG_NAME_TAG );
sandwishI = data.getExtras().getInt( Sandwish.REG_INT_TAG );
}
if ((resultCode == RESULT_OK) && (requestCode == ACTIVITY_REQUEST_CODE2)) {
snack = data.getExtras().getString( Snacks.REG_NAME_TAG);
snackI = data.getExtras().getInt( Snacks.REG_INT_TAG );
}
if ((resultCode == RESULT_OK) && (requestCode == ACTIVITY_REQUEST_CODE3)) {
juice = data.getExtras().getString( Juices.REG_NAME_TAG );
juiceI = data.getExtras().getInt( Juices.REG_INT_TAG );
}
if ((resultCode == RESULT_OK) && (requestCode == ACTIVITY_REQUEST_CODE4)) {
pizza = data.getExtras().getString( Pizza.REG_NAME_TAG );
pizzaI = data.getExtras().getInt( Pizza.REG_INT_TAG );
}
}
主要活动中的代码是从其他几个活动和按钮&#34; fab&#34;中导入字符串和整数值。将它们全部发送到另一个显示这些值的新活动。 这是从活动
导入的值的示例 public void submitOrder(View view) {
// find the user's name
CheckBox txt1CheckBox = (CheckBox) findViewById( R.id.txt1 );
boolean hasTxt1 = txt1CheckBox.isChecked();
CheckBox txt11CheckBox = (CheckBox) findViewById( R.id.txt11 );
boolean hasTxt11 = txt11CheckBox.isChecked();
boolean isTxt1 = true;
if (isTxt1) {
} else {
}
boolean isTxt11 = true;
if (isTxt11) {
} else {
}
String text = "";
if(txt1CheckBox.isChecked()) {
text += "\n" + "(" + quantity1 + ")" + " " + getString( R.string.submit_order_hamburger );
}
if(txt11CheckBox.isChecked()) {
text += "\n" + "(" + quantity2 + ")" + " " + getString( R.string.submit_order_Cheese_burger );
}
Intent i = new Intent();
i.putExtra(REG_NAME_TAG, text);
i.putExtra( REG_INT_TAG, price );
setResult(RESULT_OK,i);
finish();
}
private int calculatePrice(boolean hasTxt1, boolean hasTxt11, boolean hasTxt111, boolean hasTxt1111, boolean hasTxt11111) {
int mprice = 0;
if (hasTxt1) {
mprice = mprice + 500 * quantity1;
}
if (hasTxt11) {
mprice = mprice + 600 * quantity2;
}
return mprice;
}
这是按钮的图片及其在应用程序主要活动中的位置
答案 0 :(得分:0)
通过替换
来使用此小部件FloatingActionButton
public class FabForCart extends FloatingActionButton {
private final Property<FabForCart, Float> ANIMATION_PROPERTY =
new Property<FabForCart, Float>(Float.class, "animation") {
@Override
public void set(FabForCart object, Float value) {
mAnimationFactor = value;
postInvalidateOnAnimation();
}
@Override
public Float get(FabForCart object) {
return 0f;
}
};
private static final int MAX_COUNT = 99;
private static final String MAX_COUNT_TEXT = "99+";
private static final int TEXT_SIZE_DP = 11;
private static final int TEXT_PADDING_DP = 2;
private static final int MASK_COLOR = Color.parseColor("#33000000"); // Translucent black as mask color
private static final Interpolator ANIMATION_INTERPOLATOR = new OvershootInterpolator();
private final Rect mContentBounds;
private final Paint mTextPaint;
private final float mTextSize;
private final Paint mCirclePaint;
private final Rect mCircleBounds;
private final Paint mMaskPaint;
private final int mAnimationDuration;
private float mAnimationFactor;
private int mCount;
private String mText;
private float mTextHeight;
private ObjectAnimator mAnimator;
public FabForCart(Context context) {
this(context, null, 0);
}
public FabForCart(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FabForCart(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setUseCompatPadding(true);
final float density = getResources().getDisplayMetrics().density;
mTextSize = TEXT_SIZE_DP * density;
float textPadding = TEXT_PADDING_DP * density;
mAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
mAnimationFactor = 1;
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setStyle(Paint.Style.STROKE);
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTypeface(Typeface.SANS_SERIF);
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);
ColorStateList colorStateList = getBackgroundTintList();
if (colorStateList != null) {
mCirclePaint.setColor(colorStateList.getDefaultColor());
} else {
Drawable background = getBackground();
if (background instanceof ColorDrawable) {
ColorDrawable colorDrawable = (ColorDrawable) background;
mCirclePaint.setColor(colorDrawable.getColor());
}
}
mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mMaskPaint.setStyle(Paint.Style.FILL);
mMaskPaint.setColor(MASK_COLOR);
Rect textBounds = new Rect();
mTextPaint.getTextBounds(MAX_COUNT_TEXT, 0, MAX_COUNT_TEXT.length(), textBounds);
mTextHeight = textBounds.height();
float textWidth = mTextPaint.measureText(MAX_COUNT_TEXT);
float circleRadius = Math.max(textWidth, mTextHeight) / 2f + textPadding;
mCircleBounds = new Rect(0, 0, (int) (circleRadius * 2), (int) (circleRadius * 2));
mContentBounds = new Rect();
onCountChanged();
}
/**
* @return The current count value
*/
public int getCount() {
return mCount;
}
/**
* Set the count to show on badge
*
* @param count The count value starting from 0
*/
public void setCount(@IntRange(from = 0) int count) {
if (count == mCount) return;
mCount = count > 0 ? count : 0;
onCountChanged();
if (ViewCompat.isLaidOut(this)) {
startAnimation();
}
}
/**
* Increase the current count value by 1
*/
public void increase() {
setCount(mCount + 1);
}
/**
* Decrease the current count value by 1
*/
public void decrease() {
setCount(mCount > 0 ? mCount - 1 : 0);
}
private void onCountChanged() {
if (mCount > MAX_COUNT) {
mText = String.valueOf(MAX_COUNT_TEXT);
} else {
mText = String.valueOf(mCount);
}
}
private void startAnimation() {
float start = 0f;
float end = 1f;
if (mCount == 0) {
start = 1f;
end = 0f;
}
if (isAnimating()) {
mAnimator.cancel();
}
mAnimator = ObjectAnimator.ofObject(this, ANIMATION_PROPERTY, null, start, end);
mAnimator.setInterpolator(ANIMATION_INTERPOLATOR);
mAnimator.setDuration(mAnimationDuration);
mAnimator.start();
}
private boolean isAnimating() {
return mAnimator != null && mAnimator.isRunning();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mCount > 0 || isAnimating()) {
if (getContentRect(mContentBounds)) {
mCircleBounds.offsetTo(mContentBounds.left + mContentBounds.width() - mCircleBounds.width(), mContentBounds.top);
}
float cx = mCircleBounds.centerX();
float cy = mCircleBounds.centerY();
float radius = mCircleBounds.width() / 2f * mAnimationFactor;
// Solid circle
canvas.drawCircle(cx, cy, radius, mCirclePaint);
// Mask circle
canvas.drawCircle(cx, cy, radius, mMaskPaint);
// Count text
mTextPaint.setTextSize(mTextSize * mAnimationFactor);
canvas.drawText(mText, cx, cy + mTextHeight / 2f, mTextPaint);
}
}
private static class SavedState extends View.BaseSavedState {
private int count;
/**
* Constructor called from {@link FabForCart#onSaveInstanceState()}
*/
private SavedState(Parcelable superState) {
super(superState);
}
/**
* Constructor called from {@link #CREATOR}
*/
private SavedState(Parcel in) {
super(in);
count = in.readInt();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(count);
}
@Override
public String toString() {
return FabForCart.class.getSimpleName() + "." + SavedState.class.getSimpleName() + "{"
+ Integer.toHexString(System.identityHashCode(this))
+ " count=" + count + "}";
}
public static final Creator<SavedState> CREATOR
= new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.count = mCount;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setCount(ss.count);
requestLayout();
}
}
XML代码
<com.yourpackage.widgets.FabForCart
android:id="@+id/fabCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/_12sdp"
android:gravity="center"
app:fabSize="normal"
app:backgroundTint="@color/colorPrimaryDark"
app:layout_anchorGravity="bottom|left"
app:srcCompat="@drawable/ic_vector_shoping_cart_white" />
将此内容写入您的Java文件以设置badgeCount
fabCart.setCount(10); // set your count value from here