dailog和PopupWindow:弹窗的使用

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可以控制其显示的位置

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我把幸福的感觉记录下来 分享给你。 此时 ,你读着,就感觉到了我的幸福。 谁曾读懂过诗? 她只是用文字写下 她悲伤...
    张新怡阅读 1,612评论 4 6
  • #pragma mark ======获取当前时间戳======= - (NSString*)getCurrent...
    HAOYANG_BAI阅读 8,455评论 0 1
  • 昨晚做了一个好不可思议却又心心念念的梦,真好,不过梦都是反的吧,美好在梦里留存一下也不错,毕竟现实与想象相差太多!
    邑城灵阅读 2,555评论 0 0
  • 如果你的微信好友里有那么几个热爱家乡的潮汕人,那么前阵子肯定看过他们集体转发的一篇推文,主要内容是:俺们大潮汕要出...
    一只锅大喵阅读 6,796评论 1 5
  • https://www.jianshu.com/p/0f59b27c64d9 迎春花,是个美妙的名字,一想...
    玉暖_生烟阅读 2,187评论 0 8