安卓PopupWindow实现页面上方提示框
这里写自定义目录标题public class CustomPopWindow extends PopupWindow {private static final String TAG = "CustomPopWindow";private final View view;private Activity context;private int viewdata;public CustomPopWi
·
自己总结的一点知识,希望大佬批评指正,望后人借鉴
public class CustomPopWindow extends PopupWindow {
private static final String TAG = "CustomPopWindow";
private final View view;
private Activity context;
private int viewdata;
public CustomPopWindow(Activity context,int type,int width,int Height) {
super(context);
switch (type){
case 1:
//这里赋值自定义页面为提高代码灵活性
viewdata=R.layout.widget_popupwindow;
break;
case 2:
viewdata=R.layout.widget_popupwindow2;
break;
case 3:
viewdata=R.layout.widget_popupwindow3;
break;
case 4:
// viewdata=R.layout.widget_popupwindow4;
break;
}
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(viewdata, null);
//alt+ctrl+f
this.context = context;
initView(type);
initPopWindow(width,Height);
}
private void initView(int type) {
switch (type){
case 1:
Button cancelTv = view.findViewById(R.id.btn2);
cancelTv.setFocusable(true);
cancelTv.setFocusableInTouchMode(true);
cancelTv.requestFocus();
cancelTv.requestFocusFromTouch();
cancelTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
break;
case 2:
Button btn3 = view.findViewById(R.id.btn3);
Button btn4 = view.findViewById(R.id.btn4);
btn3.setFocusable(true);
btn3.setFocusableInTouchMode(true);
btn3.requestFocus();
btn3.requestFocusFromTouch();
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
break;
}
}
private void initPopWindow(int width ,int Height) {
this.setContentView(view);
// 设置弹出窗体的宽
this.setWidth(width);
// 设置弹出窗体的高
this.setHeight(Height);
// 设置弹出窗体可点击()
this.setFocusable(true);
this.setOutsideTouchable(true);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0x00FF00);
//设置弹出窗体的背景
this.setBackgroundDrawable(dw);
backgroundAlpha(context, 0.5f);//0.0-1.0
}
/**
* 设置添加屏幕的背景透明度(值越大,透明度越高)
*
* @param bgAlpha
*/
public void backgroundAlpha(Activity context, float bgAlpha) {
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
lp.alpha = bgAlpha;
context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
context.getWindow().setAttributes(lp);
}
}
接下来布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_corner_down">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="@color/white" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:text="点击确定"
android:background="@drawable/custombutton"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:text="点击退出"
android:background="@drawable/custombutton"
android:layout_centerInParent="true"/>
</RelativeLayout>
按钮样式
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<!-- 定义当button 处于pressed 状态时的形态。-->
<shape>
<gradient android:startColor="#8600ff" /><!--内容实现渐变-->
<stroke android:width="1dp" android:color="#000000" /><!--边框宽度和颜色-->
<corners android:radius="5dp" /><!--圆弧半径-->
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
<item android:state_focused="true">
<!-- 定义当button获得focus时的形态-->
<shape>
<gradient android:endColor="#eac100" />
<stroke android:width="2dp" android:color="#333333" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
</layer-list>
布局圆角
<?xml version="1.0" encoding="utf-8"?>
<!--定义布局圆角-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<corners android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"/>
<stroke android:width="1dp" android:color="#000000"/>
</shape>
引用
// customPopWindow=new CustomPopWindow(getActivity(),2,800,300);
// customPopWindow.showAtLocation(view,
// Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
// customPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
// @Override
// public void onDismiss() {
// customPopWindow.backgroundAlpha( getActivity(), 1f);
// }
// });
效果图
更多推荐
已为社区贡献1条内容
所有评论(0)