我正在研究如何使用 float 操作按钮 (FAB) 制作 float 操作菜单,然后我想到了 this question .在评论中,this对它的评论给出了一个提供 FloatingActionMenu 选项的类。我实现了类和 xml 文件,它只适用于一个问题。
问题:菜单中有四个 FAB,第一个是您单击以展开菜单的按钮。我希望在所有 FAB 之间有间距,但是没有间距,我不能添加任何间距。 (见下图)
FloatingActionMenu 类
public class FloatingActionMenu extends ViewGroup {
private static final long ANIMATION_DURATION = 300;
private FloatingActionButton mMenuButton;
private ArrayList<FloatingActionButton> mMenuItems;
private ArrayList<TextView> mMenuItemLabels;
private ArrayList<ItemAnimator> mMenuItemAnimators;
private int mItemMargin;
private AnimatorSet mOpenAnimatorSet = new AnimatorSet();
private AnimatorSet mCloseAnimatorSet = new AnimatorSet();
private static final int DEFAULT_CHILD_GRAVITY = Gravity.END | Gravity.BOTTOM;
private ImageView mIcon;
private boolean mOpen;
private boolean animating;
private boolean mIsSetClosedOnTouchOutside = true;
public interface OnMenuToggleListener {
void onMenuToggle(boolean opened);
}
public interface OnMenuItemClickListener {
void onMenuItemClick(FloatingActionMenu fam, int index, FloatingActionButton item);
}
private OnMenuItemClickListener onMenuItemClickListener;
private OnMenuToggleListener onMenuToggleListener;
public FloatingActionMenu(Context context) {
this(context, null, 0);
}
public FloatingActionMenu(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FloatingActionMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mMenuItems = new ArrayList<>(5);
mMenuItemAnimators = new ArrayList<>(5);
mMenuItemLabels = new ArrayList<>(5);
mIcon = new ImageView(context);
}
@Override
protected void onFinishInflate() {
bringChildToFront(mMenuButton);
bringChildToFront(mIcon);
super.onFinishInflate();
}
@Override
public void addView(@NonNull View child, int index, LayoutParams params) {
super.addView(child, index, params);
if (getChildCount() > 1) {
if (child instanceof FloatingActionButton) {
addMenuItem((FloatingActionButton) child);
}
} else {
mMenuButton = (FloatingActionButton) child;
mIcon.setImageDrawable(mMenuButton.getDrawable());
addView(mIcon);
mMenuButton.setImageDrawable(null);
createDefaultIconAnimation();
mMenuButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toggle();
}
});
}
}
public void toggle() {
if (!mOpen) {
open();
} else {
close();
}
}
public void open() {
d("open");
startOpenAnimator();
mOpen = true;
if (onMenuToggleListener != null) {
onMenuToggleListener.onMenuToggle(true);
}
}
public void close() {
startCloseAnimator();
mOpen = false;
if (onMenuToggleListener != null) {
onMenuToggleListener.onMenuToggle(true);
}
}
protected void startCloseAnimator() {
mCloseAnimatorSet.start();
for (ItemAnimator anim : mMenuItemAnimators) {
anim.startCloseAnimator();
}
}
protected void startOpenAnimator() {
mOpenAnimatorSet.start();
for (ItemAnimator anim : mMenuItemAnimators) {
anim.startOpenAnimator();
}
}
public void addMenuItem(FloatingActionButton item) {
mMenuItems.add(item);
mMenuItemAnimators.add(new ItemAnimator(item));
TextView button = new TextView(getContext());
button.setBackgroundResource(R.drawable.rounded_corners);
button.setPadding(8,8,8,8);
button.setTextColor(Color.WHITE);
button.setText(item.getContentDescription());
addView(button);
mMenuItemLabels.add(button);
item.setTag(button);
item.setOnClickListener(mOnItemClickListener);
button.setOnClickListener(mOnItemClickListener);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int width;
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int height;
final int count = getChildCount();
int maxChildWidth = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
for (int i = 0; i < mMenuItems.size(); i++) {
FloatingActionButton fab = mMenuItems.get(i);
TextView label = mMenuItemLabels.get(i);
maxChildWidth = Math.max(maxChildWidth, label.getMeasuredWidth() + fab.getMeasuredWidth() + mItemMargin);
}
maxChildWidth = Math.max(mMenuButton.getMeasuredWidth(), maxChildWidth);
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
width = maxChildWidth;
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
int heightSum = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
heightSum += child.getMeasuredHeight();
}
height = heightSum;
}
setMeasuredDimension(resolveSize(width, widthMeasureSpec),
resolveSize(height, heightMeasureSpec));
}
// Rect rect = new Rect();
// Paint paint = new Paint();
//
// @Override
// protected boolean drawChild(@NonNull Canvas canvas, @NonNull View child, long drawingTime) {
// boolean b = super.drawChild(canvas, child, drawingTime);
// paint.setColor(0xFFFF0000);
// paint.setStyle(Paint.Style.STROKE);
// rect.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
// canvas.drawRect(rect, paint);
// return b;
// }
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
if (mIsSetClosedOnTouchOutside) {
return mGestureDetector.onTouchEvent(event);
} else {
return super.onTouchEvent(event);
}
}
GestureDetector mGestureDetector = new GestureDetector(getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return mIsSetClosedOnTouchOutside && isOpened();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
close();
return true;
}
});
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
System.out.println("onLayout:" + changed);
if (changed) {
int right = r - getPaddingRight();
int bottom = b - getPaddingBottom();
int top = bottom - mMenuButton.getMeasuredHeight();
mMenuButton.layout(right - mMenuButton.getMeasuredWidth(), top, right, bottom);
int dw = (mMenuButton.getMeasuredWidth() - mIcon.getMeasuredWidth()) / 2;
int dh = (mMenuButton.getMeasuredHeight() - mIcon.getMeasuredHeight()) / 2;
mIcon.layout(right - mIcon.getMeasuredWidth() - dw, bottom - mIcon.getMeasuredHeight() - dh, right - dw, bottom - dh);
for (int i = 0; i < mMenuItems.size(); i++) {
FloatingActionButton item = mMenuItems.get(i);
TextView label = mMenuItemLabels.get(i);
bottom = top;
top -= item.getMeasuredHeight();
int width = item.getMeasuredWidth();
int d = (mMenuButton.getMeasuredWidth() - width) / 2;
item.layout(right - width - d, top, right - d, bottom);
d = (item.getMeasuredHeight() - label.getMeasuredHeight()) / 2;
label.layout(item.getLeft() - mItemMargin - label.getMeasuredWidth(), item.getTop() + d, item.getLeft() - mItemMargin, item.getTop() + d + label.getMeasuredHeight());
if (!animating) {
if (!mOpen) {
item.setTranslationY(mMenuButton.getTop() - item.getTop());
item.setVisibility(GONE);
label.setVisibility(GONE);
} else {
item.setTranslationY(0);
item.setVisibility(VISIBLE);
label.setVisibility(VISIBLE);
}
}
}
if (!animating && getBackground() != null) {
if (!mOpen) {
getBackground().setAlpha(0);
} else {
getBackground().setAlpha(0xff);
}
}
}
}
private void createDefaultIconAnimation() {
Animator.AnimatorListener listener = new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
animating = true;
}
@Override
public void onAnimationEnd(Animator animation) {
animating = false;
}
@Override
public void onAnimationCancel(Animator animation) {
animating = false;
}
@Override
public void onAnimationRepeat(Animator animation) {
}
};
ObjectAnimator collapseAnimator = ObjectAnimator.ofFloat(
mIcon,
"rotation",
135f,
0f
);
ObjectAnimator expandAnimator = ObjectAnimator.ofFloat(
mIcon,
"rotation",
0f,
135f
);
if (getBackground() != null) {
ValueAnimator hideBackgroundAnimator = ObjectAnimator.ofInt(0xff, 0);
hideBackgroundAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer alpha = (Integer) animation.getAnimatedValue();
//System.out.println(alpha);
getBackground().setAlpha(alpha > 0xff ? 0xff : alpha);
}
});
ValueAnimator showBackgroundAnimator = ObjectAnimator.ofInt(0, 0xff);
showBackgroundAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer alpha = (Integer) animation.getAnimatedValue();
//System.out.println(alpha);
getBackground().setAlpha(alpha > 0xff ? 0xff : alpha);
}
});
mOpenAnimatorSet.playTogether(expandAnimator, showBackgroundAnimator);
mCloseAnimatorSet.playTogether(collapseAnimator, hideBackgroundAnimator);
} else {
mOpenAnimatorSet.playTogether(expandAnimator);
mCloseAnimatorSet.playTogether(collapseAnimator);
}
mOpenAnimatorSet.setInterpolator(DEFAULT_OPEN_INTERPOLATOR);
mCloseAnimatorSet.setInterpolator(DEFAULT_CLOSE_INTERPOLATOR);
mOpenAnimatorSet.setDuration(ANIMATION_DURATION);
mCloseAnimatorSet.setDuration(ANIMATION_DURATION);
mOpenAnimatorSet.addListener(listener);
mCloseAnimatorSet.addListener(listener);
}
static final TimeInterpolator DEFAULT_OPEN_INTERPOLATOR = new OvershootInterpolator();
static final TimeInterpolator DEFAULT_CLOSE_INTERPOLATOR = new AnticipateInterpolator();
public boolean isOpened() {
return mOpen;
}
private class ItemAnimator implements Animator.AnimatorListener {
private View mView;
private boolean playingOpenAnimator;
public ItemAnimator(View v) {
v.animate().setListener(this);
mView = v;
}
public void startOpenAnimator() {
mView.animate().cancel();
playingOpenAnimator = true;
mView.animate().translationY(0).setInterpolator(DEFAULT_OPEN_INTERPOLATOR).start();
}
public void startCloseAnimator() {
mView.animate().cancel();
playingOpenAnimator = false;
mView.animate().translationY((mMenuButton.getTop() - mView.getTop())).setInterpolator(DEFAULT_CLOSE_INTERPOLATOR).start();
}
@Override
public void onAnimationStart(Animator animation) {
if (playingOpenAnimator) {
mView.setVisibility(VISIBLE);
} else {
((TextView) mView.getTag()).setVisibility(GONE);
}
}
@Override
public void onAnimationEnd(Animator animation) {
if (!playingOpenAnimator) {
mView.setVisibility(GONE);
} else {
((TextView) mView.getTag()).setVisibility(VISIBLE);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
}
@Override
public Parcelable onSaveInstanceState() {
d("onSaveInstanceState");
Bundle bundle = new Bundle();
bundle.putParcelable("instanceState", super.onSaveInstanceState());
bundle.putBoolean("mOpen", mOpen);
// ... save everything
return bundle;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
d("onRestoreInstanceState");
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
mOpen = bundle.getBoolean("mOpen");
// ... load everything
state = bundle.getParcelable("instanceState");
}
super.onRestoreInstanceState(state);
}
@Override
protected void onDetachedFromWindow() {
d("onDetachedFromWindow");
//getBackground().setAlpha(bgAlpha);//reset default alpha
super.onDetachedFromWindow();
}
@Override
public void setBackground(Drawable background) {
if (background instanceof ColorDrawable) {
// after activity finish and relaucher , background drawable state still remain?
int bgAlpha = Color.alpha(((ColorDrawable) background).getColor());
d("bg:" + Integer.toHexString(bgAlpha));
super.setBackground(background);
} else {
throw new IllegalArgumentException("floating only support color background");
}
}
private OnClickListener mOnItemClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v instanceof FloatingActionButton) {
int i = mMenuItems.indexOf(v);
if (onMenuItemClickListener != null) {
onMenuItemClickListener.onMenuItemClick(FloatingActionMenu.this, i, (FloatingActionButton) v);
}
} else if (v instanceof TextView) {
int i = mMenuItemLabels.indexOf(v);
if (onMenuItemClickListener != null) {
onMenuItemClickListener.onMenuItemClick(FloatingActionMenu.this, i, mMenuItems.get(i));
}
}
close();
}
};
public OnMenuToggleListener getOnMenuToggleListener() {
return onMenuToggleListener;
}
public void setOnMenuToggleListener(OnMenuToggleListener onMenuToggleListener) {
this.onMenuToggleListener = onMenuToggleListener;
}
public OnMenuItemClickListener getOnMenuItemClickListener() {
return onMenuItemClickListener;
}
public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) {
this.onMenuItemClickListener = onMenuItemClickListener;
}
protected void d(String msg) {
Log.d("FAM", msg == null ? null : msg);
}
}
菜单 XML:
<terranovaproductions.newcomicreader.FloatingActionMenu
android:id="@+id/fab_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!--First button as menu button-->
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search"
android:paddingBottom="@dimen/menu_button_margin"
fab:fabSize="normal"
android:id="@+id/fab_main" />
<!-- Other button as menu items-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/default_random"
android:src="@drawable/random"
android:paddingBottom="@dimen/menu_button_margin"
fab:fabSize="mini"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/default_search"
android:src="@drawable/search"
android:paddingBottom="@dimen/menu_button_margin"
fab:fabSize="mini"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_browser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/default_browser"
android:src="@drawable/browseropen"
android:paddingBottom="@dimen/menu_button_margin"
fab:fabSize="mini"/>
</terranovaproductions.newcomicreader.FloatingActionMenu>
此 XML 代码与其他项目位于 RelativeLayout 中。
我尝试过的:
最佳答案
似乎这段代码完全忽略了项目填充。我想一个快速而肮脏的方法是修改 onLayout 并硬编码你想要的填充。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
System.out.println("onLayout:" + changed);
if (changed) {
int right = r - getPaddingRight();
int bottom = b - getPaddingBottom();
int top = bottom - mMenuButton.getMeasuredHeight();
mMenuButton.layout(right - mMenuButton.getMeasuredWidth(), top, right, bottom);
int dw = (mMenuButton.getMeasuredWidth() - mIcon.getMeasuredWidth()) / 2;
int dh = (mMenuButton.getMeasuredHeight() - mIcon.getMeasuredHeight()) / 2;
mIcon.layout(right - mIcon.getMeasuredWidth() - dw, bottom - mIcon.getMeasuredHeight() - dh, right - dw, bottom - dh);
for (int i = 0; i < mMenuItems.size(); i++) {
FloatingActionButton item = mMenuItems.get(i);
TextView label = mMenuItemLabels.get(i);
bottom = top -= 10; //Add 10px padding
top -= item.getMeasuredHeight();
int width = item.getMeasuredWidth();
int d = (mMenuButton.getMeasuredWidth() - width) / 2;
item.layout(right - width - d, top, right - d, bottom);
d = (item.getMeasuredHeight() - label.getMeasuredHeight()) / 2;
label.layout(item.getLeft() - mItemMargin - label.getMeasuredWidth(), item.getTop() + d, item.getLeft() - mItemMargin, item.getTop() + d + label.getMeasuredHeight());
if (!animating) {
if (!mOpen) {
item.setTranslationY(mMenuButton.getTop() - item.getTop());
item.setVisibility(GONE);
label.setVisibility(GONE);
} else {
item.setTranslationY(0);
item.setVisibility(VISIBLE);
label.setVisibility(VISIBLE);
}
}
}
if (!animating && getBackground() != null) {
if (!mOpen) {
getBackground().setAlpha(0);
} else {
getBackground().setAlpha(0xff);
}
}
}
}
关于android - FloatingActionMenu 类在按钮之间添加间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467493/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_