androidUI——PopWindow
2015-11-09 18:44
344 查看
3个文件,MainActivity.java,activity_main.xml,popup_window.xml
MainActivity:
activity_main.xml:
popup_window.xml:
MainActivity:
package com.example.popwindowdemo; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.Toast; /** * * @author H100 * PopWindow和AlertDialogDialog的区别 * Alertdialog居中显示,PopWindow不居中显示,显示在指定位置 * AlertDialog会阻塞线程,比如Ui,PopWindow不会 * */ public class MainActivity extends Activity implements OnClickListener{ Button btn_one,btn_three,btn_at; PopupWindow pop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initButton(); initPopwindow(); } void initButton(){ btn_one=(Button) findViewById(R.id.btnShowAsDrawDown); btn_three=(Button) findViewById(R.id.btnShowAsDrawDown1); btn_at=(Button) findViewById(R.id.btnShowAt); btn_at.setOnClickListener(this); btn_one.setOnClickListener(this); btn_three.setOnClickListener(this); } void initPopwindow(){ View view=LayoutInflater.from(this).inflate(R.layout.popup_window, null); /** * 关于layoutParams的设置方法一般就是设置父控件的LayoutParams,下面的竟然也能用,也就是父父的也可以 */ pop=new PopupWindow(view, LinearLayout.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); pop.setOutsideTouchable(true); Button btn=(Button) view.findViewById(R.id.btn_pop); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); Toast.makeText(MainActivity.this, "popwindow手动退出", 500).show(); } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnShowAsDrawDown: if(pop.isShowing()){ pop.dismiss(); }else{ pop.showAsDropDown(v); } break; case R.id.btnShowAsDrawDown1: if(pop.isShowing()){ pop.dismiss(); }else{ pop.showAsDropDown(v,0,-320);//起始点向上平移320 } break; case R.id.btnShowAt: if(pop.isShowing()){ pop.dismiss(); }else{ //为了让这个水平平移效果不被FillParent遮盖就干脆设置成Wrap pop.setWindowLayoutMode(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 100, 50); } break; default: break; } } }
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main" android:orientation="vertical" tools:context="com.example.popwindowdemo.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btnShowAsDrawDown" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show as drawdown(one parameter)" /> <Button android:id="@+id/btnShowAsDrawDown1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show as drawdown(three parameter)" /> <Button android:id="@+id/btnShowAt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show at location" /> </LinearLayout>
popup_window.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ff9aaab7"> <!-- 突出显示所以设置颜色要不就透明了 --> <Button android:id="@+id/btn_pop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="dismiss"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="none"/> </LinearLayout>
相关文章推荐
- IOS UITest 初始化 ViewController
- 自定义 UIButton(图片+文字)
- 使用values文件夹dimens文件做适配的一点想法
- iOS小技巧-UINavigationItem代码设置导航栏返回按钮的字体
- GacUI源码简析(一)
- 举一反三 willDisplayCell在UICollectionView中的一些应用
- UIViewAnimationOptions类型
- JSP内置对象--request对象 (setCharacterEncoding("GBK"),getParameter(),getParameterValues(),getParameterNames(),getServletPath(),getContextPath()
- UIButton 图标与文字上下垂直对齐
- 一些有用的configue参数
- 关于 UIView 的 layoutSubviews 方法
- servlet中request中的函数
- IOS-25-xib由原来的UIViewController改为UIView后程序崩溃
- hdu4424 Conquer a New Region
- Building QT for i.MX6 with YOCTO
- Ueditor/自定义配置
- UI:实现ScrollView能够下拉刷新效果
- UI 学习 第一章 UIWindow UIview
- MySQL MetaData中PRI_KEY,PART_KEY,MULTIPLE_KEY,UNIQUE_KEY的内涵
- ArrayList,stack,queue