PopupWindow实现弹窗,可以任意自定义布局
2016-09-18 22:59
330 查看
用于实现弹窗,可以任意自定义布局,android中弹窗可以分为AlertDialog和popupWindow,AlertDialog只能显示固定在中间位置,popupWindow可以在任意位置,相对更加灵活。
1.主布局设置
2.自定义弹窗布局
3.代码实现
//为弹窗提供自定义的布局
//创建弹窗PopupWindow
//设置相关的属性
//显示弹窗
1.主布局设置
<Button android:id="@+id/button_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" android:textSize="20sp" android:onClick="submitClick"/>
2.自定义弹窗布局
<Button android:id="@+id/button_update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="更新" android:textSize="20sp" android:layout_weight="1"/> <Button android:id="@+id/button_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" android:textSize="20sp" android:layout_weight="1"/>
3.代码实现
//为弹窗提供自定义的布局
//创建弹窗PopupWindow
//设置相关的属性
//显示弹窗
private View button_update; private View button_delete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击button事件 public void submitClick(View view){ showSubmitClick(view); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void showSubmitClick(View view) { View v=getLayoutInflater().inflate(R.layout.popup_window,null); //加载的布局 button_update = v.findViewById(R.id.button_update); button_delete = v.findViewById(R.id.button_delete); //创建弹窗popupwindow PopupWindow popupWindow =new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,true); //设置背景 popupWindow.setBackgroundDrawable(getDrawable(R.mipmap.index_intro_bg)); BitmapDrawable bitmapDrawable =new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(),R.mipmap.index_intro_bg)); //设置透明度 popupWindow.getBackground().setAlpha(80); //设置动画效果 popupWindow.setAnimationStyle(android.R.anim.fade_in); //设置点击弹窗外面消失 popupWindow.setOutsideTouchable(true); //设置防止软键盘遮挡popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MODE_CHANGED); //显示弹窗 popupWindow.showAsDropDown(view); //设置点击事件 button_delete.setOnClickListener(this); button_update.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "更新成功", Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); }
相关文章推荐
- android自定义RadioGroup可以添加多种布局的实现方法
- android自定义RadioGroup实现可以添加多种布局
- android自定义RadioGroup实现可以添加多种布局
- RadioGroup - android自定义RadioGroup实现可以添加多种布局
- BottomDialog 是一个通过 DialogFragment 实现的底部弹窗布局,并且支持弹出动画,支持任意布局http://shaohui.me
- Android自定义ViewGroup自动换行实现滑动任意布局及事件处理效果
- Android编程实现的自定义弹窗(PopupWindow)功能示例
- Android开发实现popupWindow弹出窗口自定义布局与位置控制方法
- Fragment+ListView实现碎片化管理订单(另附自定义布局AlertDialog弹窗的设置)
- 自定义ViewGroup实现流式布局(支持ViewGroup Padding, 子View margin,每行高度可以不一样)
- 用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)
- android自定义RadioGroup实现可以添加多种布局
- 在Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示。
- android自定义RadioGroup实现可以添加多种布局
- Android自定义View设定到FrameLayout布局中实现多组件显示
- JQuery实现个性化,可以拖拽 自定义自己的界面(一)
- Silverlight:利用Panel实现自定义布局
- JQuery实现个性化,可以拖拽 自定义自己的界面(三)
- JQuery实现个性化,可以拖拽 自定义自己的界面(二)
- 让自定义的类型可以和任意的类型之间转换