一句话搞定高仿ios底部弹出提示框(Android)
最近项目里面用到了底部的弹出提示框,UI小姐姐本着设计样式还是ios的好看原则。设计了一个ios样式的底部弹出提示框。OK OK anyway,类似样式并不少见,实现方式有很多,网上随便找一个吧,还不满大街都是。嗯哼,确实不少。但是 !!! 不是讲代码就是讲布局,或者使用方法挺麻烦。
用的时候还要自己手写这部分代码,麻烦不麻烦?作为一名注定要改变世界的程序猿,你让我天天写这个?这是不能忍的。就没有简单的,快捷的,高效的,一句话就能搞定的吗?
有需求就有产品,所以,这个BottomMenu产生了。GitHub项目地址
先来看下效果:
How to use:
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency
dependencies { compile 'com.github.zhaolei9527:BottomMenu:v1.0.1' }
Activity文件代码:(一句话,有木有?很简单,有木有?)
基本用法:
new BottomMenuFragment(MainActivity.this) .addMenuItems(new MenuItem("从相册选择")) .addMenuItems(new MenuItem("拍照")) .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() { @Override public void onItemClick(TextView menu_item, int position) { Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } }) .show();
带Title用法:
new BottomMenuFragment(MainActivity.this) .setTitle("标题") .addMenuItems(new MenuItem("从相册选择")) .addMenuItems(new MenuItem("拍照")) .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() { @Override public void onItemClick(TextView menu_item, int position) { Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } }) .show();
指定条目样式用法:
new BottomMenuFragment(MainActivity.this) .setTitle("标题") .addMenuItems(new MenuItem("从相册选择", MenuItem.MenuItemStyle.COMMON)) .addMenuItems(new MenuItem("拍照", MenuItem.MenuItemStyle.STRESS)) .setOnItemClickListener(new BottomMenuFragment.OnItemClickListener() { @Override public void onItemClick(TextView menu_item, int position) { Toast.makeText(MainActivity.this, menu_item.getText().toString().trim(), Toast.LENGTH_SHORT).show(); } }) .show();
全部一句话搞定,还有更多功能可以自己发掘一下。
最后看下组件代码:
整体结构为三个文件,BottomMenuFragment为弹出主体内容,MenuItem为条目对象的POJO,MenuItemAdapter顾名思义是条目的适配器。代码逻辑实现方法其实挺简单,大同小异,只不过对于代码进行封装使操作更加便捷,简单且迅速。
BottomMenuFragment文件:
public class BottomMenuFragment extends DialogFragment { private final String TAG = "BottomMenuFragment"; private Activity context; private OnItemClickListener mOnItemClickListener; private boolean showTitle = false; private String BottomTitle = ""; public BottomMenuFragment(Activity context) { this.context = context; } private List<MenuItem> menuItemList = new ArrayList<MenuItem>(); public List<MenuItem> getMenuItems() { return menuItemList; } public void addMenuItems(List<MenuItem> menuItems) { this.menuItemList.addAll(menuItems); } public BottomMenuFragment addMenuItems(MenuItem menuItems) { menuItemList.add(menuItems); return this; } public BottomMenuFragment setTitle(String BottomTitle) { showTitle = true; this.BottomTitle = BottomTitle; return this; } public void show() { this.show(context.getFragmentManager(), "BottomMenuFragment"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//设置背景透明 getDialog().getWindow().setWindowAnimations(R.style.menu_animation);//添加一组进出动画 View view = inflater.inflate(R.layout.fragment_bottom_menu, container, false); //view.setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.menu_appear));//添加一个加载动画,这样的问题是没办法添加消失动画,有待进一步研究 ((TextView) view.findViewById(tv_cancel)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "onClick: tv_cancel"); BottomMenuFragment.this.dismiss(); } }); if (showTitle) { menuItemList.add(0, new MenuItem(BottomTitle, MenuItem.MenuItemStyle.COMMON)); } ListView lv_menu = (ListView) view.findViewById(R.id.lv_menu); MenuItemAdapter menuItemAdapter = new MenuItemAdapter(getActivity().getBaseContext(), this.menuItemList); lv_menu.setAdapter(menuItemAdapter); lv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.i(TAG, "onClick: "); if (mOnItemClickListener != null) { if (showTitle) { if (position == 0) { return; } } TextView menu_item = (TextView) view.findViewById(R.id.menu_item); mOnItemClickListener.onItemClick(menu_item, position); dismiss(); } } }); return view; } public interface OnItemClickListener { void onItemClick(TextView menu_item, int position); } public BottomMenuFragment setOnItemClickListener(@Nullable OnItemClickListener listener) { mOnItemClickListener = listener; return this; } @Override public void onDestroyView() { super.onDestroyView(); Log.i(TAG, "onDestroyView: "); } @Override public void onAttach(Context context) { super.onAttach(context); Log.i(TAG, "onAttach: "); } @Override public void onDetach() { super.onDetach(); Log.i(TAG, "onDetach: "); } @Override public void onStart() { super.onStart(); Log.i(TAG, "onStart: "); //设置弹出框宽屏显示,适应屏幕宽度 DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height); //移动弹出菜单到底部 WindowManager.LayoutParams wlp = getDialog().getWindow().getAttributes(); wlp.gravity = Gravity.BOTTOM; // wlp.width = WindowManager.LayoutParams.MATCH_PARENT; getDialog().getWindow().setAttributes(wlp); } @Override public void onStop() { this.getView().setAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.menu_disappear)); super.onStop(); } }
MenuItemAdapter文件:
public class MenuItemAdapter extends BaseAdapter { private Context context;//运行上下文 private LayoutInflater listContainer; //视图容器 private List<MenuItem> menuItems; public MenuItemAdapter(Context _context, List<MenuItem> _menuItems){ this.context = _context; this.listContainer = LayoutInflater.from(_context); this.menuItems = _menuItems; } @Override public int getCount() { return this.menuItems.size(); } @Override public Object getItem(int position) { if(position >= menuItems.size() || position < 0) { return null; } else { return menuItems.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if(convertView == null) { view = listContainer.inflate(R.layout.menu_item, null); } MenuItem menuItem = menuItems.get(position); TextView textView = (TextView) view.findViewById(R.id.menu_item); textView.setText(menuItem.getText()); if(menuItems.size() == 1) { textView.setBackgroundResource(R.drawable.bottom_menu_btn_selector); } else if(position == 0) { textView.setBackgroundResource(R.drawable.bottom_menu_top_btn_selector); } else if(position < menuItems.size() - 1) { textView.setBackgroundResource(R.drawable.bottom_menu_mid_btn_selector); } else { textView.setBackgroundResource(R.drawable.bottom_menu_bottom_btn_selector); } if(menuItem.getStyle() == MenuItem.MenuItemStyle.COMMON) { textView.setTextColor(ContextCompat.getColor(context, R.color.bottom_menu_btn_text_commom_color)); } else { textView.setTextColor(ContextCompat.getColor(context, R.color.bottom_menu_btn_text_stress_color)); } return view; } }
MenuItem文件:
public class MenuItem { public MenuItem() { } /** * @param _item_name 菜单项名称 * @param _text 菜单项显示内容 * @param _style 菜单类型 */ public MenuItem(String _item_name, String _text, MenuItemStyle _style) { this.item_name = _item_name; this.text = _text; this.style = _style; } public MenuItem(String _text, MenuItemStyle _style) { this.text = _text; this.style = _style; } public MenuItem(String _text) { this.text = _text; } private String item_name; private String text; private MenuItemStyle style = MenuItemStyle.COMMON; public String getItem_name() { return item_name; } public String getText() { return text; } public MenuItemStyle getStyle() { return style; } public void setItem_name(String item_name) { this.item_name = item_name; } public void setText(String text) { this.text = text; } /** * @param style 菜单类型 */ public void setStyle(MenuItemStyle style) { this.style = style; } public enum MenuItemStyle { COMMON, STRESS } }
总结
代码整体满足了一句话搞定高仿ios底部弹出提示框的功能,当然,
有了需求才有了功能,有了想法才有了创作,你的反馈会是使我进步的最大动力。
觉得还不够方便?还想要什么功能?告诉我!欢迎反馈,欢迎Star。源码入口:BottomMenu-GitHub项目地址
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android-来填写一个验证码吧!(一)
熟悉web开发中童鞋们都知道为了防止恶意破解、恶意提交等行为,所以我们在提交表单数据时,都会使用随机验证码功能。在Android应用中我们同样需要这一功能,去有效的避开恶意注册,恶意攻击。那么该如何实现呢? 这里介绍一种,数字英文随机生成的图片验证码,使用很简单,一行代码,调用一个工具类,就能完美的实现图片验证的显示了。 效果如下: **具体思路如下:在一块固定宽高的画布上,画上固定个数的随机数字和字母,再画上固定条数的干扰线随机数和干扰线的颜色随机生成,随机数的样式随机生成。** 理清思路,接下来看下代码吧! public class CodeUtils { //随机码集 private static final char[] CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', '...
- 下一篇
一句话搞定Android底部导航栏,一键绑定Fragment、ViewPager
现在大多数App都会用到底部导航栏,比如常见的聊天工具QQ、微信、购物App等等,有了底部导航栏,用户可以随时切换界面,查看不同的内容。它的实现方式也很多,以前大多使用TabHost来实现,但是现在我们有很多更好的选择。 常见的底部导航栏实现方式:1.使用LinearLayout + TextView实现了底部导航栏的效果2.使用RadioGroup + RadioButton实现了底部导航栏的效果3.利用BottomNavigationBar实现底部导航栏....... 实现方式有很多,不过好像都只是关于下边Tab切换而已,没有实现Tab与界面的联动,用的时候还要自己手写这部分代码,麻烦不麻烦? 作为一名注定要改变世界的程序猿,你让我天天写这个?这是不能忍的。 就不能有个东西,能够一句话搞定Android底部导航栏,一键绑定Fragment、ViewPager吗? 所以,这个BottomTabBar产生了。GitHub项目地址 先来看下效果: How to use: Step 1. Add the JitPack repository to your build fileAdd it...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS8安装Docker,最新的服务器搭配容器使用
- 设置Eclipse缩进为4个空格,增强代码规范
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS7,8上快速安装Gitea,搭建Git服务器