美文网首页
dailog和PopupWindow:弹窗的使用

dailog和PopupWindow:弹窗的使用

作者: 谜之龙 | 来源:发表于2019-04-08 14:05 被阅读0次

1.dailog的使用(全屏使用)

package com.example.zhilongzhang.allme.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.zhilongzhang.allme.R;

public class PublicDialog extends Dialog {
Context mContext;
btnInterface listener;
TextView tvMseeage;
String message="提示语";
public interface btnInterface {
    void btnConfirm();
}

public PublicDialog(Context context,String  message,btnInterface listener) {
    super(context, R.style.Dialog_public);
    this.mContext = context;
    this.listener = listener;
    this.message = message;

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_style1);
    //设置点击其他地方弹窗是否消失
    this.setCanceledOnTouchOutside(true);
    fullWidth();
    initData();
}
//让弹窗的宽度充满屏幕
private void fullWidth() {
    Window window = getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.gravity = Gravity.BOTTOM; //底部
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    window.setAttributes(lp);
}
private void initData(){
    findViewById(R.id.bt_confirm).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.btnConfirm();
        }
    });
    findViewById(R.id.bt_cancle).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    tvMseeage = findViewById(R.id.tv_message);
    tvMseeage.setText(message);
}
}

Style的样式

 <style name="Dialog_public">
    <item name="android:windowBackground">@color/noColor</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

页面布局

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#60f5f598">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:layout_centerInParent="true"
    android:layout_margin="20dp">
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:text="提示语"
        android:gravity="center"
        android:layout_margin="20dp"/>
    <Button
        android:id="@+id/bt_cancle"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:text="取消"
        android:layout_below="@+id/tv_message"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"/>
    <Button
        android:id="@+id/bt_confirm"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:text="确定"
        android:layout_below="@+id/tv_message"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"/>
</RelativeLayout>
</RelativeLayout>

2.PopupWindow的使用

package com.example.zhilongzhang.allme.dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import com.example.zhilongzhang.allme.R;

public class MePopup extends PopupWindow implements View.OnClickListener {
private Context mContext;
private OnItemOnClickListener mItemOnClickListener;

public interface OnItemOnClickListener {
    void onItemClick(int position);
    void onClencl();
}

public MePopup(Context context) {
    this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

public MePopup(Context context, int width, int height) {
    this.mContext = context;
    setFocusable(true);
    setTouchable(true);
    setOutsideTouchable(true);
    setWidth(width);
    setHeight(height);
    setBackgroundDrawable(new ColorDrawable());
    setContentView(LayoutInflater.from(mContext).inflate(R.layout.view_popup, null));
    initUI();
    //设置动画所对应的style
//        setAnimationStyle(R.style.popAniStyle);
}
int zongWidth=200;
private void initUI() {
    getContentView().findViewById(R.id.zong_popup).post(new Runnable() {
        @Override
        public void run() {
            zongWidth=getContentView().findViewById(R.id.zong_popup).getWidth();
            Log.e("das","zong="+zongWidth);
        }
    });
}

public void setItemOnClickListener(OnItemOnClickListener onItemOnClickListener) {
    this.mItemOnClickListener = onItemOnClickListener;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.popup1:
            mItemOnClickListener.onItemClick(0);
            break;
        case R.id.popup2:
            mItemOnClickListener.onClencl();
            break;
    }
}

public void show(int position, final View v, final int width) {
    switch (1) {
        //在指定view的正左下方,无偏移
        case 0:
            showAsDropDown(v);
            break;
        //相对指定view正左下方的位置,有偏移,第一个参数是横向X,第二个是纵向Y
        case 1:
            //zongWidth代表弹窗的宽度表示弹窗显示在右边
            showAsDropDown(v, width -zongWidth, 0);
            break;
    }
}
}

布局

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/zong_popup"
android:background="#f0fa7c">
 <TextView
   android:id="@+id/popup1"
   android:layout_width="100dp"
   android:layout_height="wrap_content"
   android:layout_marginTop="10dp"
   android:layout_marginBottom="10dp"
   android:gravity="center"
   android:text="第一个"/>
<View
    android:layout_width="100dp"
    android:layout_height="1dp"
    android:background="#000000"/>
<TextView
    android:id="@+id/popup2"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    android:text="第二个"/>
</LinearLayout>

用法

 MePopup    popup=new MePopup(ShowDialogActivity.this);
            popup.setItemOnClickListener(new MePopup.OnItemOnClickListener() {
                @Override
                public void onItemClick(int position) {

                }

                @Override
                public void onClencl() {

                }
            });

            popup.show(0,findViewById(R.id.tv4),poppWidth);

其中的show可以控制其显示的位置

相关文章

  • dailog和PopupWindow:弹窗的使用

    1.dailog的使用(全屏使用) Style的样式 页面布局 2.PopupWindow的使用 布局 用法 其中...

  • PopupWindow使用详解

    概述 PopupWindow,顾名思义,就是弹窗. 基本用法 使用PopupWindow可以总结为三个步骤: 创建...

  • Android PopupWindow

    显示PopupWindow 设置弹窗外的背景颜色 弹窗进入和退出的动画 进入动画 退出动画

  • [Android]自定义定时消失PopupWindow

    使用CountDownTimer类监听PopupWindow, 实现提示弹窗消息,让用户不用点击过段时间自动消失,...

  • 控件基础应用

    Android 5.0以上系统常用控件着色指南 通用PopupWindow,几行代码搞定PopupWindow弹窗

  • 关于弹窗Dialog,Toast,PopupWindow,Sna

    关于弹窗Dialog,Toast,PopupWindow,SnackBar总结分析目录介绍:0.关于弹窗概述1.关...

  • 2019-01-06

    Android封装一个通用的PopupWindow 效果:弹窗在android的每个项目中基本上都会使用到的,主要...

  • popupWindow的封装与学习

      本篇主要富含了对PopupWindow的封装,实现动画弹窗的例子。可用于相册选择、点赞等等。同时封装使用了建造...

  • Android中PopupWindow,ListPopupWin

    在开发项目中会经常遇到弹窗选择的情况,这时就会用到PopupWindow,PopupWindow的用法比较简单,但...

  • android获取屏幕高度

    两台手机做dailog弹窗高度自定义时发现始终适配不了,经测试发现 getResources().getDispl...

网友评论

      本文标题:dailog和PopupWindow:弹窗的使用

      本文链接:https://www.haomeiwen.com/subject/krmuiqtx.html