android效果TapBarMenu绘制底部导航栏的使用方式示例

其他的不多说了!我们来看看效果吧

     

一、实现方式一:直接引入compile方式

Add the dependency to your build.gradle:

compile ‘com.github.michaldrabik:tapbarmenu:1.0.5'

布局设计

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/dark_gray"
 tools:context=".MainActivity">
 <!--中间按钮颜色-->
 <!--app:tbm_backgroundColor="@color/red"-->

 <!--是否初始进入页面就可以看到item图片-->
 <!--app:tbm_showItems="true"-->

 <!--中间按钮大小-->
 <!--app:tbm_buttonSize="30dp"-->

 <!--中间按钮位置-->
 <!--app:tbm_buttonPosition="center"-->

 <!--中间按钮位置左边距-->
 <!--app:tbm_buttonMarginLeft="0dp"-->

 <!--中间按钮位置右边距-->
 <!--app:tbm_buttonMarginRight="0dp"-->

 <!--中间按钮自定义图标打开状态。必须是一个向量可拉的动画。-->
 <!-- app:tbm_iconOpened="@drawable/icon"-->

 <!--中间按钮自定义图标关闭状态。必须是一个向量可拉的动画。-->
 <!--app:tbm_iconOpened="@drawable/icon"-->

 <!--中间按钮打卡item显示位置-->
 <!--app:tbm_menuAnchor="bottom"-->
 <com.michaldrabik.tapbarmenulib.TapBarMenu
  android:id="@+id/tapBarMenu"
  android:layout_width="match_parent"
  android:layout_height="56dp"
  android:layout_alignParentBottom="true"
  android:layout_marginBottom="24dp"
  app:tbm_backgroundColor="@color/red"
  app:tbm_buttonMarginLeft="0dp"
  app:tbm_buttonMarginRight="0dp"
  app:tbm_buttonPosition="center"
  app:tbm_buttonSize="30dp"
  app:tbm_iconClosed="@drawable/icon"
  app:tbm_iconOpened="@drawable/icon"
  app:tbm_menuAnchor="bottom"
  app:tbm_showItems="false">

  <ImageView
   android:id="@+id/item1"
   android:layout_width="0dp"
   android:layout_height="match_parent"
   android:layout_weight="1"
   android:paddingBottom="10dp"
   android:paddingTop="10dp"
   android:src="@drawable/ic_person"
   tools:visibility="visible" />

  <ImageView
   android:id="@+id/item2"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:paddingBottom="10dp"
   android:paddingTop="10dp"
   android:src="@drawable/ic_location" />

  <Space
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1" />

  <ImageView
   android:id="@+id/item3"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:paddingBottom="10dp"
   android:paddingTop="10dp"
   android:src="@drawable/ic_thumb_up" />

  <ImageView
   android:id="@+id/item4"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:paddingBottom="10dp"
   android:paddingTop="10dp"
   android:src="@drawable/ic_thumb_down" />

 </com.michaldrabik.tapbarmenulib.TapBarMenu>

</RelativeLayout>

在Activity中的代码

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.michaldrabik.tapbarmenulib.TapBarMenu;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

 @Bind(R.id.tapBarMenu)
 TapBarMenu tapBarMenu;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ButterKnife.bind(this);
 }

 private boolean isClick = true;

 @OnClick(R.id.tapBarMenu)
 public void onMenuButtonClick() {
//  if (isClick) {
//   tapBarMenu.toggle();
//   isClick = false;
//  }
  tapBarMenu.toggle();
 }

 @OnClick({R.id.item1, R.id.item2, R.id.item3, R.id.item4})
 public void onMenuItemClick(View view) {
// tapBarMenu.close();
  switch (view.getId()) {
   case R.id.item1:
    Toast.makeText(this,"item1",Toast.LENGTH_LONG).show();
    break;
   case R.id.item2:
    Toast.makeText(this,"item2",Toast.LENGTH_LONG).show();
    break;
   case R.id.item3:
    Toast.makeText(this,"item3",Toast.LENGTH_LONG).show();
    break;
   case R.id.item4:
    Toast.makeText(this,"item4",Toast.LENGTH_LONG).show();
    break;
  }
 }
}

到这里效果就基本实现了

二、实现方式二:引入Module方式

module中记得引入

compile ‘com.wnafee:vector-compat:1.0.5'

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import com.wnafee.vector.compat.ResourcesCompat;

/**
 * TapBar Menu Layout.
 *
 * @author Michal Drabik (michal.drabik0@gmail.com) on 2015-11-13.
 */
public class TapBarMenu extends LinearLayout {

 public static final int BUTTON_POSITION_LEFT = 0;
 public static final int BUTTON_POSITION_CENTER = 1;
 public static final int BUTTON_POSITION_RIGHT = 2;
 public static final int MENU_ANCHOR_BOTTOM = 3;
 public static final int MENU_ANCHOR_TOP = 4;
 private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator(2.5f);

 private enum State {
  OPENED,
  CLOSED
 }

 private static final int LEFT = 0;
 private static final int RIGHT = 1;
 private static final int TOP = 2;
 private static final int BOTTOM = 3;
 private static final int RADIUS = 4;

 private AnimatorSet animatorSet = new AnimatorSet();
 private ValueAnimator[] animator = new ValueAnimator[5];
 private float[] button = new float[5];

 private Path path = new Path();
 private State state = State.CLOSED;
 private Paint paint;
 private int animationDuration;
 private float width;
 private float height;
 private float buttonLeftInitial;
 private float buttonRightInitial;
 private float yPosition;
 private Drawable iconOpenedDrawable;
 private Drawable iconClosedDrawable;
 private OnClickListener onClickListener;

 //Custom XML Attributes
 private int backgroundColor;
 private int buttonSize;
 private int buttonPosition;
 private int buttonMarginRight;
 private int buttonMarginLeft;
 private int menuAnchor;
 private boolean showMenuItems;

 public TapBarMenu(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(attrs);
 }

 public TapBarMenu(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init(attrs);
 }

 private void init(AttributeSet attrs) {
  setWillNotDraw(false);
  setupAttributes(attrs);
  setGravity(Gravity.CENTER);
  setupAnimators();
  setupPaint();
 }

 private void setupAttributes(AttributeSet attrs) {
  TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TapBarMenu, 0, 0);

  if (typedArray.hasValue(R.styleable.TapBarMenu_tbm_iconOpened)) {
   iconOpenedDrawable = typedArray.getDrawable(R.styleable.TapBarMenu_tbm_iconOpened);
  } else {
   iconOpenedDrawable = ResourcesCompat.getDrawable(getContext(), R.drawable.icon_animated);
  }

  if (typedArray.hasValue(R.styleable.TapBarMenu_tbm_iconClosed)) {
   iconClosedDrawable = typedArray.getDrawable(R.styleable.TapBarMenu_tbm_iconClosed);
  } else {
   iconClosedDrawable = ResourcesCompat.getDrawable(getContext(), R.drawable.icon_close_animated);
  }

  backgroundColor = typedArray.getColor(R.styleable.TapBarMenu_tbm_backgroundColor, ContextCompat.getColor(getContext(), R.color.red));
  buttonSize =
    typedArray.getDimensionPixelSize(R.styleable.TapBarMenu_tbm_buttonSize, getResources().getDimensionPixelSize(R.dimen.defaultButtonSize));
  buttonMarginRight = typedArray.getDimensionPixelSize(R.styleable.TapBarMenu_tbm_buttonMarginRight, 0);
  buttonMarginLeft = typedArray.getDimensionPixelSize(R.styleable.TapBarMenu_tbm_buttonMarginLeft, 0);
  buttonPosition = typedArray.getInt(R.styleable.TapBarMenu_tbm_buttonPosition, BUTTON_POSITION_CENTER);
  menuAnchor = typedArray.getInt(R.styleable.TapBarMenu_tbm_menuAnchor, MENU_ANCHOR_BOTTOM);
  showMenuItems = typedArray.getBoolean(R.styleable.TapBarMenu_tbm_showItems, false);
  typedArray.recycle();
 }

 private void setupAnimators() {
  for (int i = 0; i < 5; i++) {
   animator[i] = new ValueAnimator();
  }

  animator[LEFT].addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    button[LEFT] = (float) valueAnimator.getAnimatedValue();
   }
  });
  animator[RIGHT].addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    button[RIGHT] = (float) valueAnimator.getAnimatedValue();
   }
  });
  animator[TOP].addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    button[TOP] = (float) valueAnimator.getAnimatedValue();
   }
  });
  animator[BOTTOM].addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    button[BOTTOM] = (float) valueAnimator.getAnimatedValue();
   }
  });
  animator[RADIUS].addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator valueAnimator) {
    button[RADIUS] = (float) valueAnimator.getAnimatedValue();
    invalidate();
   }
  });
  animationDuration = getResources().getInteger(R.integer.animationDuration);
  animatorSet.setDuration(animationDuration);
  animatorSet.setInterpolator(DECELERATE_INTERPOLATOR);
  animatorSet.playTogether(animator);
 }

 private void setupMenuItems() {
  for (int i = 0; i < getChildCount(); i++) {
   getChildAt(i).setVisibility(showMenuItems ? VISIBLE : GONE);
  }
 }

 private void setupPaint() {
  paint = new Paint();
  paint.setColor(backgroundColor);
  paint.setAntiAlias(true);
 }

 @Override
 protected void onAttachedToWindow() {
  super.onAttachedToWindow();
  setupMenuItems();
 }

 /**
  * Opens the menu if it's closed or close it if it's opened.
  */
 public void toggle() {
  if (state == State.OPENED)
   close();
  else
   open();
 }

 /**
  * Open the menu.
  */
 public void open() {
  state = State.OPENED;
  showIcons(true);

  animator[LEFT].setFloatValues(button[LEFT], 0);
  animator[RIGHT].setFloatValues(button[RIGHT], width);
  animator[RADIUS].setFloatValues(button[RADIUS], 0);
  animator[TOP].setFloatValues(button[TOP], 0);
  animator[BOTTOM].setFloatValues(button[BOTTOM], height);

  animatorSet.cancel();
  animatorSet.start();
  if (iconOpenedDrawable instanceof Animatable) {
   ((Animatable) iconOpenedDrawable).start();
  }
  ViewGroup parentView = (ViewGroup) TapBarMenu.this.getParent();
  this.animate()
    .y(menuAnchor == MENU_ANCHOR_BOTTOM ? parentView.getBottom() - height : 0)
    .setDuration(animationDuration)
    .setInterpolator(DECELERATE_INTERPOLATOR)
    .start();
 }

 /**
  * Close the menu.
  */
 public void close() {
  updateDimensions(width, height);
  state = State.CLOSED;
  showIcons(false);

  animator[LEFT].setFloatValues(0, button[LEFT]);
  animator[RIGHT].setFloatValues(width, button[RIGHT]);
  animator[RADIUS].setFloatValues(0, button[RADIUS]);
  animator[TOP].setFloatValues(0, button[TOP]);
  animator[BOTTOM].setFloatValues(height, button[BOTTOM]);

  animatorSet.cancel();
  animatorSet.start();
  if (iconClosedDrawable instanceof Animatable) {
   ((Animatable) iconClosedDrawable).start();
  }
  this.animate()
    .y(yPosition)
    .setDuration(animationDuration)
    .setInterpolator(DECELERATE_INTERPOLATOR)
    .start();
 }

 /**
  * @return True if menu is opened. False otherwise.
  */
 public boolean isOpened() {
  return state == State.OPENED;
 }

 /**
  * Sets TapBarMenu's background color from given resource.
  *
  * @param colorResId Color resource id. For example: R.color.holo_blue_light
  */
 public void setMenuBackgroundColor(int colorResId) {
  backgroundColor = ContextCompat.getColor(getContext(), colorResId);
  paint.setColor(backgroundColor);
  invalidate();
 }

 /**
  * Set position of 'Open Menu' button.
  *
  * @param position One of: {@link #BUTTON_POSITION_CENTER}, {@link #BUTTON_POSITION_LEFT}, {@link #BUTTON_POSITION_RIGHT}.
  */
 public void setButtonPosition(int position) {
  buttonPosition = position;
  invalidate();
 }

 /**
  * Sets diameter of 'Open Menu' button.
  *
  * @param size Diameter in pixels.
  */
 public void setButtonSize(int size) {
  buttonSize = size;
  invalidate();
 }

 /**
  * Sets left margin for 'Open Menu' button.
  *
  * @param margin Left margin in pixels
  */
 public void setButtonMarginLeft(int margin) {
  buttonMarginLeft = margin;
 }

 /**
  * Sets right margin for 'Open Menu' button.
  *
  * @param margin Right margin in pixels
  */
 public void setButtonMarginRight(int margin) {
  buttonMarginRight = margin;
 }

 /**
  * Set anchor point of the menu. Can be either bottom or top.
  *
  * @param anchor One of: {@link #MENU_ANCHOR_BOTTOM}, {@link #MENU_ANCHOR_TOP}.
  */
 public void setAnchor(int anchor) {
  menuAnchor = anchor;
 }

 /**
  * Sets the passed drawable as the drawable to be used in the open state.
  *
  * @param openDrawable The open state drawable
  */
 public void setIconOpenDrawable(Drawable openDrawable) {
  this.iconOpenedDrawable = openDrawable;
  invalidate();
 }

 /**
  * Sets the passed drawable as the drawable to be used in the closed state.
  *
  * @param closeDrawable The closed state drawable
  */
 public void setIconCloseDrawable(Drawable closeDrawable) {
  this.iconClosedDrawable = closeDrawable;
  invalidate();
 }

 /**
  * Sets the passed drawable as the drawable to be used in the open state.
  *
  * @param openDrawable The open state drawable
  */
 public void setIconOpenedDrawable(Drawable openDrawable) {
  this.iconOpenedDrawable = openDrawable;
  invalidate();
 }

 /**
  * Sets the passed drawable as the drawable to be used in the closed state.
  *
  * @param closeDrawable The closed state drawable
  */
 public void setIconClosedDrawable(Drawable closeDrawable) {
  this.iconClosedDrawable = closeDrawable;
  invalidate();
 }

 @Override
 public void setOnClickListener(OnClickListener listener) {
  onClickListener = listener;
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  updateDimensions(w, h);
  yPosition = getY();
 }

 @Override
 protected void onDraw(Canvas canvas) {
  canvas.drawPath(createRoundedRectPath(button[LEFT], button[TOP], button[RIGHT], button[BOTTOM], button[RADIUS], button[RADIUS], false), paint);
  if (state == State.CLOSED) {
   iconClosedDrawable.draw(canvas);
  } else {
   iconOpenedDrawable.draw(canvas);
  }
 }

 private void updateDimensions(float w, float h) {
  int ratio;
  width = w;
  height = h;

  button[RADIUS] = buttonSize;
  setButtonPosition(width);
  if (iconClosedDrawable instanceof Animatable) {
   ratio = 3;
  } else {
   ratio = 5;
  }
  float iconLeft = button[LEFT] + buttonSize / ratio;
  float iconTop = (height - buttonSize) / 2 + buttonSize / ratio;

  float iconRight = button[RIGHT] - buttonSize / ratio;
  float iconBottom = (height + buttonSize) / 2 - buttonSize / ratio;
  iconOpenedDrawable.setBounds((int) iconLeft, (int) iconTop, (int) iconRight, (int) iconBottom);
  iconClosedDrawable.setBounds((int) iconLeft, (int) iconTop, (int) iconRight, (int) iconBottom);
 }

 private void setButtonPosition(float width) {
  if (buttonPosition == BUTTON_POSITION_CENTER) {
   button[LEFT] = ((width / 2) - (buttonSize / 2));
  } else if (buttonPosition == BUTTON_POSITION_LEFT) {
   button[LEFT] = 0;
  } else {
   button[LEFT] = width - buttonSize;
  }
  int padding = buttonMarginLeft - buttonMarginRight;
  button[LEFT] += padding;
  button[RIGHT] = button[LEFT] + buttonSize;
  button[TOP] = (height - buttonSize) / 2;
  button[BOTTOM] = (height + buttonSize) / 2;
  buttonLeftInitial = button[LEFT];
  buttonRightInitial = button[RIGHT];
 }

 private void showIcons(final boolean show) {
  for (int i = 0; i < getChildCount(); i++) {
   final View view = getChildAt(i);
   int translation = menuAnchor == MENU_ANCHOR_BOTTOM ? view.getHeight() : -view.getHeight();
   if (show){
    view.setTranslationY(Float.valueOf(translation));
   }else {
    view.setTranslationY(0f);
   }
//   view.setTranslationY(show ? translation : 0f);
   view.setScaleX(show ? 0f : 1f);
   view.setScaleY(show ? 0f : 1f);
   view.setVisibility(VISIBLE);
   view.setAlpha(show ? 0f : 1f);
   view.animate()
     .scaleX(show ? 1f : 0f)
     .scaleY(show ? 1f : 0f)
     .translationY(0f)
     .alpha(show ? 1f : 0f)
     .setInterpolator(DECELERATE_INTERPOLATOR)
     .setDuration(show ? animationDuration / 2 : animationDuration / 3)
     .setStartDelay(show ? animationDuration / 3 : 0)
     .setListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
       super.onAnimationEnd(animation);
       view.setVisibility(show ? VISIBLE : GONE);
      }
     })
     .start();
  }
 }

 private Path createRoundedRectPath(float left, float top, float right, float bottom, float rx, float ry, boolean conformToOriginalPost) {
  path.reset();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   return createRoundedRectPathApi21(path, left, top, right, bottom, rx, ry, conformToOriginalPost);
  } else {
   return createRoundedRectPathPreApi21(path, left, top, right, bottom, rx, ry, conformToOriginalPost);
  }
 }

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 private Path createRoundedRectPathApi21(Path path, float left, float top, float right, float bottom, float rx, float ry, boolean
   conformToOriginalPost) {
  if (rx < 0) rx = 0;
  if (ry < 0) ry = 0;
  float width = right - left;
  float height = bottom - top;
  if (rx > width / 2) rx = width / 2;
  if (ry > height / 2) ry = height / 2;
  float widthMinusCorners = (width - (2 * rx));
  float heightMinusCorners = (height - (2 * ry));
  path.moveTo(right, top + ry);
  path.arcTo(right - 2 * rx, top, right, top + 2 * ry, 0, -90, false);
  path.rLineTo(-widthMinusCorners, 0);
  path.arcTo(left, top, left + 2 * rx, top + 2 * ry, 270, -90, false);
  path.rLineTo(0, heightMinusCorners);
  if (conformToOriginalPost) {
   path.rLineTo(0, ry);
   path.rLineTo(width, 0);
   path.rLineTo(0, -ry);
  } else {
   path.arcTo(left, bottom - 2 * ry, left + 2 * rx, bottom, 180, -90, false);
   path.rLineTo(widthMinusCorners, 0);
   path.arcTo(right - 2 * rx, bottom - 2 * ry, right, bottom, 90, -90, false);
  }
  path.rLineTo(0, -heightMinusCorners);
  path.close();
  return path;
 }

 private Path createRoundedRectPathPreApi21(Path path, float left, float top, float right, float bottom, float rx, float ry, boolean
   conformToOriginalPost) {
  if (rx < 0) rx = 0;
  if (ry < 0) ry = 0;
  float width = right - left;
  float height = bottom - top;
  if (rx > width / 2) rx = width / 2;
  if (ry > height / 2) ry = height / 2;
  float widthMinusCorners = (width - (2 * rx));
  float heightMinusCorners = (height - (2 * ry));
  path.moveTo(right, top + ry);
  path.rQuadTo(0, -ry, -rx, -ry);
  path.rLineTo(-widthMinusCorners, 0);
  path.rQuadTo(-rx, 0, -rx, ry);
  path.rLineTo(0, heightMinusCorners);
  if (conformToOriginalPost) {
   path.rLineTo(0, ry);
   path.rLineTo(width, 0);
   path.rLineTo(0, -ry);
  } else {
   path.rQuadTo(0, ry, rx, ry);
   path.rLineTo(widthMinusCorners, 0);
   path.rQuadTo(rx, 0, rx, -ry);
  }
  path.rLineTo(0, -heightMinusCorners);
  path.close();
  return path;
 }

 @Override
 public boolean onInterceptTouchEvent(MotionEvent event) {
  return (event.getX() > buttonLeftInitial && event.getX() < buttonRightInitial);
 }

 @Override
 public boolean onTouchEvent(@NonNull MotionEvent event) {
  if ((event.getX() > buttonLeftInitial && event.getX() < buttonRightInitial) && (event.getAction() == MotionEvent.ACTION_UP)) {
   if (onClickListener != null) {
    onClickListener.onClick(this);
   }
  }
  return true;
 }

 @Override
 protected void onDetachedFromWindow() {
  onDestroy();
  super.onDetachedFromWindow();
 }

 private void onDestroy() {
  iconOpenedDrawable = null;
  iconClosedDrawable = null;
  for (int i = 0; i < 5; i++) {
   animator[i] = null;
  }
  animator = null;
  button = null;
  onClickListener = null;
 }
}

布局和代码与上面一致,module请下载:

链接:http://pan.baidu.com/s/1o8jlnpo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android中PopupMenu组件的使用实例

    最近学习研究了一下Android中PopupMenu组件的使用,发现很实用,所以留个笔记留作日后查询 估计很多人遇到过这种场景: 要求弹出的PopupWindow里面是一个列表,我们使用时都是在里面套个ListView或RecyclerView ,现在我们不需要在做这样繁琐的工作了. 在官方android.support.v7.widget 包下提供的 PopupMenu 组件,已经被越来越多的项目所采用.我们先看一下几个 app 的效果: 这是一个非常轻量化的上下文菜单组件,简洁.使用方便.

  • Android中menu使用详解

    Menu(菜单)是Android中一定会使用的模块,每个Android项目都会用到Menu来给用户起到选择和导航的作用,提升用户体验,下面通过本文给大家分享android 中menu使用,需要的朋友一起看看吧 很多activity界面中都存在一个菜单栏,就是点击右上角的一个按钮的时候会出现一个下拉列表差不多的东西,这个功能的实现其实只需要下面的两步,每一个activity都可以拥有自己独一无二的menu,具体的格式可以自己进行定义,详细的创建步骤如下 ①在res下的menu中创建file_men

  • Android使用onCreateOptionsMenu()创建菜单Menu的方法详解

    本文实例讲述了Android使用onCreateOptionsMenu()创建菜单Menu的方法.分享给大家供大家参考,具体如下: Android一共有三种形式的菜单: 1.选项菜单(optinosMenu) 2.上下文菜单(ContextMenu) 3.子菜单(subMenu) 其中最常用的就是选项菜单(optionsMenu), 该菜单在点击 menu 按键 后会在对应的Activity底部显示出来. 1.Activity菜单机制 (与dialog类似) Activity有一套机制来实现对菜

  • Android 开源项目侧边栏菜单(SlidingMenu)使用详解

    项目下载地址:https://github.com/jfeinstein10/SlidingMenu 注意: SlidingMenu依赖于另一个开源项目ActionBarSherlock,所以需要将ActionBarSherlock添加作为SlidingMenu的库工程,否则会报资源找不到错误. 然后再将SlidingMenu添加到自己的工程中去. SlidingMenu集成常见错误: Jar mismatch! Fix your dependencies :引用的工程和自身工程以来的jar包版

  • Android使用Item Swipemenulistview实现仿QQ侧滑删除功能

    大家都用过QQ,肯定有人好奇QQ滑动删除Item的效果是怎样实现的,其实我们使用Swipemenulistview就可以简单的实现.先看看我们项目中的效果: 使用的时候可以把Swipemenulistview作为一个library,也可以把Swipemenulistview的源码拷贝到我们的项目中来,使用步骤大致可以分为三步:1.在布局中配置:2.在Java代码中初始化配置:3.按钮点击事件的处理  1.在布局中配置 xml布局文件中只需要简单使用这个自定义的ListView就行了,需要注意的是

  • Android开源组件SlidingMenu侧滑菜单使用介绍

    现在很多android应用都有侧滑菜单,效果很不错. GitHub上有SlidingMenu的开源库,使用起来很方便. SlidingMenu GitHub地址:https://github.com/jfeinstein10/SlidingMenu.GitHub上说,Sliding结合ActionBarSherlock使用功能可以更丰富,ActionBarSherlock GitHub地址:https://github.com/JakeWharton/ActionBarSherlock 附csd

  • Android仿微信菜单(Menu)(使用C#和Java分别实现)

    本篇是对安卓菜单使用编程方式实现,当然可以使用XML的方式完成同样的功能,基本Java和C#写法都是一致的,所以使用XML的方式在本篇中使用Java演示,需要注意的是,对于如果不是VS开发的话,那么资源文件名称必须以小写开头,否则会报错. 运行效果 C#实现 using Android.App; using Android.OS; using Android.Views; using Android.Widget; namespace MenuDemo { [Activity(Label = "

  • android效果TapBarMenu绘制底部导航栏的使用方式示例

    其他的不多说了!我们来看看效果吧       一.实现方式一:直接引入compile方式 Add the dependency to your build.gradle: compile 'com.github.michaldrabik:tapbarmenu:1.0.5' 布局设计 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sc

  • 解决android 显示内容被底部导航栏遮挡的问题

    描述: 由于产品需求,要求含有EditText的界面全屏显示,最好的解决方式是使用AndroidBug5497Workaround.assistActivity(this) 的方式来解决,但是华为和魅族手机系统自带的有底部导航栏,会造成一些布局被遮挡. 解决方案:在values-21的style.xml中添加android:windowDrawsSystemBarBackgrounds"并将值设置为false,方式如下 在style引用的主题里面加入android:windowDrawsSyst

  • Android开发快速实现底部导航栏示例

    目录 Tint 着色器 依赖(AndroidX) 布局 编写渲染颜色选择器-tint_selector_menu_color menu 文件中 icon-nav_bottom_menu BottomNavigationView的点击事件 配合ViewPager实现Tab栏 对应的适配器 Tint 着色器 优点:去除“无用”图片,节省空间 配合BottomNavigationView,实现一个快速,简洁的Tab栏 传统做法:Tab 切换,字体变色.图片变色.至少给我提供八张图,四张默认,四张选中,

  • Android使用RadioGroup实现底部导航栏

    RadioGroup实现底部导航栏效果,如图:: 实现可最基本的导航栏功能,不能左右滑动,只能点击 1.内嵌的fragment的布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical&q

  • Android使用BottomNavigationBar实现底部导航栏

    本文实例为大家分享了Android实现底部导航栏的具体代码,供大家参考,具体内容如下 展示 MODE_FIXED+BACKGROUND_STYLE_STATIC效果 DE_FIXED+BACKGROUND_STYLE_RIPPLE效果 MODE_SHIFTING+BACKGROUND_STYLE_STATIC效果 MODE_SHIFTING+BACKGROUND_STYLE_RIPPLE效果 1在Gradle中添加 compile 'com.ashokvarma.android:bottom-n

  • Android实现美团外卖底部导航栏动画

    体验了一下美团外卖的底部导航栏,感觉动画很流畅,分割线被顶起,还有图标的动画,可能用的lottie,觉得分割线被顶起可以自己写动画,所以试着写了一下 . 想自定义view点击实现动画效果,自定义view的区域一定比背景需要被顶起的线要高,所以布局如下: 开始绘制view,被顶起的曲线分三段,前后两端曲线对称的,用path绘制曲线,中间段绘制贝塞尔曲线. 那么我们分别绘制三段曲线,用ValueAnimator实现效果, private void initAnim() { value = start

  • 微信小程序实战之仿android fragment可滑动底部导航栏(4)

    底部3-5个选项的底部导航栏,目前在移动端上是主流布局之一,因此腾讯官方特地做了,可以通过设置,就可以做出了一个底部的导航栏. 相关教程:微信小程序教程系列之设置标题栏和导航栏(7) 但是通过设置的这个底部的导航栏,功能上比较固定,它必须要设置与它对应的一个页面,而且并不能滑动. 在业务上,有时候会比较限制,并不能完全满足所需. 又例如早前有人拿着UI稿问我,这种广告轮播图的样式,在小程序能不能实现呢? 我当时没有想了下,还不是很确定,因为小程序的轮播图的那几个小点点实在比较普通,样式单一. 因

  • Flutter底部导航栏的实现方式

    本文实例为大家分享了Flutter底部导航栏的实现代码,供大家参考,具体内容如下 老规格,先看图: 程序主结构如下: 1.在程序主入口文件main.dart添加如下代码 import 'package:flutter/material.dart'; import 'bottom_navigation.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build

  • android实现简单底部导航栏

    本文实例为大家分享了android实现底部导航栏的具体代码,供大家参考,具体内容如下 常见的底部导航栏 动态效果 实现步骤 1.底部导航栏样式 我们应该在项目的res文件夹下新建一个menu文件夹,用来装menu布局文件 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"&g

  • Android实现简单底部导航栏 Android仿微信滑动切换效果

    Android仿微信滑动切换最终实现效果: 大体思路: 1. 主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标; 2. 底部导航栏的设置方法类似于TabLayout的关联,View需要创建关联方法,用来关联VIewPager; 3. 通过关联方法获取ViewPager实例后,根据ViewPager页面数创建底部导航栏的图标按钮; 代码实现: 1. 新建第一个自定义View, 图标 + 文字 的底部按钮; /** * 自定义控件

随机推荐