#Android笔记#基于popupwindow的底部菜单栏设计与功能实现
2014-10-15 21:52
585 查看
底部菜单栏是一个应用相当广泛的控件,尤其对于类似于电子书一类的需要配置大量参数的应用来说。最近学习中就遇到了这样一个例子,有必要自己记录一下。
这是模仿多看阅读器做的一个demo,当然功能还没有做全,目前只完成了字体大小,屏幕亮度,背景切换以及行间距设置的功能,以后还会进一步完善。
言归正传,看一下代码,首先是自定义的popupwindow:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
代码解释:
首先初始化一个inflater对象,然后通过inflater将layout布局与自定义的一个view绑定,这样就可以通过这个自定义的View来关联layout布局上的控件了。
接着再来看看主界面MainActivity的代码:
几个功能的实现代码都没有什么特别的难度,就不多说了。到此为止,这个底部菜单栏的雏形就构成了!
总结:对于笔者这个初学者来说,这个demo的收获就是了解了LayoutInflater对于自定义View的作用:是一个将layout布局和自定义View绑定的媒介,绑定以后,就可以获取layout布局上的控件,并且使得它们能够发挥作用。
这是模仿多看阅读器做的一个demo,当然功能还没有做全,目前只完成了字体大小,屏幕亮度,背景切换以及行间距设置的功能,以后还会进一步完善。
言归正传,看一下代码,首先是自定义的popupwindow:
public class SelectPicPopupWindow extends PopupWindow { private Button fontSize1, fontSize2, btn_narrow,btn_normal,btn_wide; private ImageView bgcolor1,bgcolor2,bgcolor3,bgcolor4,bgcolor5; private View mMenuView; private SeekBar seekBar; public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick,OnSeekBarChangeListener onSeekBarChangeListener) { super(context); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.alert_dialog, null); fontSize1 = (Button)mMenuView.findViewById(R.id.fontSize1);
//此处省略了相关控件获取id的代码
seekBar = (SeekBar)mMenuView.findViewById(R.id.seekBar1); fontSize1.setOnClickListener(itemsOnClick);
//此处省略了相关控件设置监听的代码
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
//页面布局设置 this.setContentView(mMenuView); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setFocusable(true); //设置弹出方式(动画) this.setAnimationStyle(R.style.AnimBottom); ColorDrawable dw = new ColorDrawable(0xb0000000); this.setBackgroundDrawable(dw); //设置点击屏幕其他位置,菜单栏消失 mMenuView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop(); int y=(int) event.getY(); if(event.getAction()==MotionEvent.ACTION_UP){ if(y<height){ dismiss(); } } return true; } }); } public static void setBrightness(Activity activity, int brightness) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = brightness / 255f; activity.getWindow().setAttributes(lp); } }这里要提醒一下自己,LayoutInflater的作用:它是一个载入界面的工具,相当于findViewByid,只不过后者是对于xml下的控件而言的。
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMenuView = inflater.inflate(R.layout.alert_dialog, null);
代码解释:
首先初始化一个inflater对象,然后通过inflater将layout布局与自定义的一个view绑定,这样就可以通过这个自定义的View来关联layout布局上的控件了。
接着再来看看主界面MainActivity的代码:
import android.widget.Toast; public class MainActivity extends Activity { private int curFontSize = 20;//初始化字体大小 private SelectPicPopupWindow menuWindow; private RelativeLayout main; private float lineSpace = 1f;//初始化行间距 TextView demo;//用于测试的TextView @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LayoutInflater layoutInflater = LayoutInflater.from(this); final TextView tv = (TextView) this.findViewById(R.id.text); demo = (TextView)this.findViewById(R.id.demo); main = (RelativeLayout)findViewById(R.id.main); demo.setLineSpacing(20, lineSpace); demo.setTextSize(curFontSize);
//通过点击tv弹出对话框 tv.setOnClickListener(new OnClickListener() { public void onClick(View v) { menuWindow = new SelectPicPopupWindow(MainActivity.this,itemsOnClick,onSeekBarChangeListener); menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); } }); }
//设置屏幕调节进度条的监听 private OnSeekBarChangeListener onSeekBarChangeListener = new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) { // TODO Auto-generated method stub setBrightness(MainActivity.this, progress); } };
//各按钮的监听: private OnClickListener itemsOnClick = new OnClickListener(){ public void onClick(View v) { switch (v.getId()) {
//设置行间距 case R.id.btn_narrow: lineSpace = 0.5f; demo.setLineSpacing(20, lineSpace); break; case R.id.btn_normal: lineSpace = 1f; demo.setLineSpacing(20, lineSpace); break; case R.id.btn_wide: lineSpace = 1.5f; demo.setLineSpacing(20, lineSpace); break;
//设置字体大小 case R.id.fontSize1: if(curFontSize>10){ curFontSize--; demo.setTextSize(curFontSize); } break; case R.id.fontSize2: if(curFontSize<=30){ curFontSize++; demo.setTextSize(curFontSize); } break;
//设置背景风格
case R.id.bgcolor1: main.setBackgroundColor(Color.parseColor("#9d9e9f")); demo.setTextColor(Color.parseColor("#FFFFFF")); break; case R.id.bgcolor2: main.setBackgroundColor(Color.parseColor("#668b8b")); demo.setTextColor(Color.parseColor("#FFFFFF")); break; case R.id.bgcolor3: main.setBackgroundColor(Color.parseColor("#00688b")); demo.setTextColor(Color.parseColor("#FFFFFF")); break; case R.id.bgcolor4: demo.setTextColor(Color.parseColor("#A0522D")); main.setBackgroundColor(Color.parseColor("#FFE4C4")); break; case R.id.bgcolor5: demo.setTextColor(Color.parseColor("#000000")); main.setBackgroundColor(Color.parseColor("#a2dca2")); break; default: break; } } };
//调节屏幕亮度的方法 public static void setBrightness(Activity activity, int brightness) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = brightness / 255f; activity.getWindow().setAttributes(lp); } }
几个功能的实现代码都没有什么特别的难度,就不多说了。到此为止,这个底部菜单栏的雏形就构成了!
总结:对于笔者这个初学者来说,这个demo的收获就是了解了LayoutInflater对于自定义View的作用:是一个将layout布局和自定义View绑定的媒介,绑定以后,就可以获取layout布局上的控件,并且使得它们能够发挥作用。
相关文章推荐
- android 倒计时、有清除功能编辑框、popupwindow、跑马灯效果实现(基于TextView)
- #Android笔记#fragment+fragmentTabHost实现底部菜单栏
- Android 底部菜单栏Tab功能的几种实现方式
- 【Android安全研究笔记】基于行为模式的Android平台入侵检测系统的设计与实现
- Android 底部菜单栏Tab功能的几种实现方式
- 基于Struts2和Android的简单登陆注册功能的设计与实现(待完善)
- android底部悬浮菜单栏的设计与实现
- Android HAL实现的三种方式(2) - 基于Service的HAL设计
- android实现底部菜单栏
- Android HAL实现的三种方式(2) - 基于Service的HAL设计
- Android HAL实现的三种方式(2) - 基于Service的HAL设计
- Android HAL实现的三种方式(3) - 基于Manager的HAL设计
- android实现底部菜单栏
- Android HAL实现的三种方式(1) - 基于JNI的简单HAL设计 推荐
- 基于Android源码开发的来电接通振动功能实现
- 基于角色-功能-资源的权限控制模型的设计与实现-引子
- 基于JAVA的水泥三维图像特征提取系统设计与实现 之一 JAVA 3D学习笔记
- android实现底部菜单栏
- 基于Android源码开发的来电接通振动功能实现
- 基于Android的传感器和语音识别的设计与实现