PopupWindow简单使用
具体代码:
public void showPopWin() {
View popupWindow_view = getLayoutInflater().inflate(R.layout.popwindown, null,
false);
/**
* 创建popupWindow实例,-1代表:match_parent -2代表:wrap_content
*/
popupWindow = new PopupWindow(popupWindow_view, -1, -2);
View view = findViewById(R.id.vv_video);
//使popupwindow显示在view的下方
popupWindow.showAsDropDown(view);
}
注意:
有一个很重要的问题,popupWindow不能显示在onCreate()或者Activity任何生命周期内,因为可能Activity没有构建完,添加不了popupWindow,所以尽可能将popupWindow写道点击事件内,或者发送个延迟消息,例如
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
showPopWin();
break;
}
}
};
//在onCreate()中发送个延迟消息
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shaketv);
ButterKnife.inject(this);
handler.sendEmptyMessageDelayed(0, 1000);
}
浙公网安备 33010602011771号