lolafon 2014-11-11
这是一个点击删除按钮,弹出PopupWindow对话框操作的案例,昨天第一次用PopupWindow,拿出来分享下:
xml页面如下:
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_dialog"
/>
</LinearLayout>
window.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffeeaa"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#cccccc"
android:text="删除"/>
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="#ffee99"
android:alpha="0.3"/>
</LinearLayout>
唯一的Activity:MainActivity,不用手动注册什么
packagecom.example.popupwindow;
importandroid.support.v7.app.ActionBarActivity;
importandroid.support.v7.app.ActionBar;
importandroid.support.v4.app.Fragment;
importandroid.app.ActionBar.LayoutParams;
importandroid.app.Activity;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.os.Bundle;
importandroid.util.DisplayMetrics;
importandroid.view.LayoutInflater;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.widget.Button;
importandroid.widget.ImageView;
importandroid.widget.PopupWindow;
importandroid.widget.Toast;
importandroid.os.Build;
importandroid.provider.Settings.System;
publicclassMainActivityextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageViewbtn=(ImageView)findViewById(R.id.btn);
btn.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//显示窗口
LayoutInflaterinflater=LayoutInflater.from(MainActivity.this);
//引入窗口配置文件
Viewview=inflater.inflate(R.layout.window,null);
//创建PopupWindow对象
finalPopupWindowpop=newPopupWindow(view,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,false);
DisplayMetricsmetric=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
intwidth=metric.widthPixels;
pop.setWidth(width);
pop.setHeight(140);
pop.setAnimationStyle(android.R.style.Animation_InputMethod);
//需要设置一下此参数,点击外边可消失
pop.setBackgroundDrawable(newBitmapDrawable());
//设置点击窗口外边窗口消失
pop.setOutsideTouchable(true);
//设置此参数获得焦点,否则无法点击
pop.setFocusable(true);
if(pop.isShowing()){
//隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏
pop.dismiss();
}else{
pop.showAsDropDown(v);
}
//得到子页面,从而得到需要添加监听的控件
Vieww=pop.getContentView();
ButtonbtnConfig=(Button)w.findViewById(R.id.button1);
btnConfig.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
Toast.makeText(MainActivity.this,"删除",Toast.LENGTH_SHORT).show();
}
});
Buttonbtn=(Button)w.findViewById(R.id.button2);
btn.setOnClickListener(newOnClickListener(){
@Override
publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
pop.dismiss();
}
});
}
});
}
}