import android.content.Context;android
import android.graphics.Color;ide
import android.graphics.drawable.ColorDrawable;動畫
import android.view.Gravity;this
import android.view.LayoutInflater;spa
import android.view.View;xml
import android.view.WindowManager;事件
import android.widget.Button;utf-8
import android.widget.LinearLayout;get
import android.widget.PopupWindow;it
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>