import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.qudoulicai.www.qqlistview.R;
/**
* Created by Lenovo on 2015/11/23.
*/
public class PopupwindowDemo {
private Context context;
//构造方法传入Context
public PopupwindowDemo(Context context) {
this.context = context;
}
public void ShowPopuwindow(View views) {
//设置显示内容
LinearLayout lin = new LinearLayout(context);
lin.setOrientation(LinearLayout.VERTICAL);
Button button1 = new Button(context);
button1.setText("内容");
Button button2 = new Button(context);
button2.setText("城市");
lin.addView(button1);
lin.addView(button2);
第一个参数是代表要显示的view,第二个参数是设置显示宽度满屏,第三个高度自适应
final PopupWindow window = new PopupWindow(lin, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//设置单击其他位置window消失
window.setFocusable(true);
ColorDrawable dm = new ColorDrawable(Color.WHITE);
window.setBackgroundDrawable(dm);
//设置动画。稍后贴上
window.setAnimationStyle(R.style.Myanmi);
//参数:第一个在activity中单击控件的view,第二个,在屏幕哪个地方显示,第三第四个是显示的位置(0,0)默认不设置位置
window.showAtLocation(views, Gravity.BOTTOM, 0, 0);
//window中的单击事件
Button button = (Button) view.findViewById(R.id.first);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
window.dismiss();
}
});
}
}
//隐藏动画
<?xml version="1.0" encoding="UTF-8"?>
-<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:toYDelta="50%p" android:fromYDelta="0" android:duration="500"/>
<alpha android:duration="500" android:toAlpha="0.0" android:fromAlpha="1.0"/>
</set>
//显示动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>