安卓学习UI组件-PopupWindow-弹出窗口

安卓学习UI组件-PopupWindow-弹出窗口_第1张图片

安卓学习UI组件-PopupWindow-弹出窗口_第2张图片

安卓学习UI组件-PopupWindow-弹出窗口_第3张图片

 

案例效果:安卓学习UI组件-PopupWindow-弹出窗口_第4张图片

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.myapplication.MainActivity">


<Button
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showWindow"
android:text="显示弹出窗口" />
</RelativeLayout>

popup_window_layout.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">


<Button
android:id="@+id/button_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="编辑" />

<Button
android:id="@+id/button_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除" />
</LinearLayout>

MainActivity.java

 

package com.example.administrator.myapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

//PopupWindow
public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//按钮事件方法
public void showWindow(View v)
{

View view=getLayoutInflater().inflate(R.layout.popup_window_layout,null);
//实例化创建popupWindow(窗体的视图,宽,高)对象
PopupWindow popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);


popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);//设置动画效果
popupWindow.getBackground().setAlpha(100);//设置透明度
popupWindow.setOutsideTouchable(true);//点击边上时候可以消失
popupWindow.setFocusable(true);//当前窗口获得焦点
popupWindow.setTouchable(true);//可以被触摸
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//防止虚拟软键盘被弹出菜单遮住

popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);


//获取设备屏幕尺寸,4.1之后使用
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;



//按钮事件
Button button_edit= (Button)view.findViewById(R.id.button_edit);
Button button_delete= (Button)view.findViewById(R.id.button_delete);

button_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你点击的是编辑操作",Toast.LENGTH_SHORT).show();
}
});

button_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"你点击的是删除操作",Toast.LENGTH_SHORT).show();
}
});

}
}

你可能感兴趣的:(安卓学习UI组件-PopupWindow-弹出窗口)