PopupWindow

PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。
如何自定义窗体
构造方法:public PopupWindow (Context context):context上下文对象.

3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度

窗体:
显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);

public class Main2Activity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//TODO 去除自带的bar
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
imageView = (ImageView) findViewById(R.id.add);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_popupwindow();
}
});
}
//弹出窗体
public void show_popupwindow(){
//TODO 1:实例化对象
PopupWindow popupWindow = new PopupWindow(Main2Activity.this);
//TODO 2:设置属性
View view= LayoutInflater.from(this).inflate(R.layout.layout_weixin_popupwindow,null);
popupWindow.setContentView(view);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(600);
//设置点击外部消失
popupWindow.setOutsideTouchable(true);
//TODO 3:展示
popupWindow.showAsDropDown(imageView,-100,-100);
}
}

底部弹窗
public void click(View view) {
show_popupwindow();
}
//弹出窗体
public void show_popupwindow(){
//TODO 1:实例化对象
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
//TODO 2:设置属性
View view=LayoutInflater.from(this).inflate(R.layout.layout3,null);
final TextView textView = view.findViewById(R.id.camera_tv);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView.getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});
popupWindow.setContentView(view);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
//设置点击外部消失
popupWindow.setOutsideTouchable(true);
//TODO 3:展示
/**
* @param parent 父布局
* @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
* @param x x轴偏移量
* @param y y轴偏移量
*/
View parent=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);
}

透明度
PopupWindow popupWindow= new PopupWindow(Main3Activity.this);
popupWindow.setHeight(300);
popupWindow.setWidth(400);
View view1=LayoutInflater.from(Main3Activity.this).inflate(R.layout.pop,null);
popupWindow.setContentView(view1);
//设置点击外部,窗体消失
popupWindow.setOutsideTouchable(true);
//1.获得当前窗体的属性
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
//2.设置透明度为0.5f,半透明状态
layoutParams.alpha=0.5f;
//3.为窗体设置新属性
getWindow().setAttributes(layoutParams);
//4.当窗体消失的时候,恢复透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha=1f;
getWindow().setAttributes(layoutParams);
}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值