Android实现一个显示中间的dialog

效果展示

在这里插入图片描述

xml编写

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="@dimen/dp_275"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/shape_pop_authority_application_ll"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="@dimen/dp_275"
            android:layout_height="wrap_content"
         />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_16"
            android:layout_marginBottom="@dimen/dp_10">

           
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/dp_4"
                android:text="测试测试"
                android:textSize="@dimen/sp_12"
                tools:ignore="HardcodedText" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_25"
            android:orientation="horizontal">


            <TextView
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="拒绝"
                android:textColor="@color/gray_69"
                android:textSize="@dimen/sp_14"
                tools:ignore="HardcodedText" />


            <View
                android:layout_width="0.5dp"
                android:layout_height="match_parent"
                android:background="@color/border_line1" />


            <TextView
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="同意"
                android:textColor="@color/color_protocol"
                android:textSize="@dimen/sp_14"
                tools:ignore="HardcodedText" />


        </LinearLayout>

    </LinearLayout>


</FrameLayout>

MainActivity.java

private AlertDialog dialogProtocol;

private void dialogProtocol() {

        AlertDialog.Builder builder = new AlertDialog.Builder(AuctionActivity.this);
        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(AuctionActivity.this).inflate(
                R.layout.pppp, null);


        //拒绝
        TextView tv_pop_blue_words_protocol_cancel = view.findViewById(R.id.tv_pop_blue_words_protocol_cancel);
        tv_pop_blue_words_protocol_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //同意
        TextView tv_pop_blue_words_protocol_fix = view.findViewById(R.id.tv_pop_blue_words_protocol_fix);
        tv_pop_blue_words_protocol_fix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogProtocol.cancel();
            }
        });

        builder.setView(view);
        dialogProtocol = builder.create();
        dialogProtocol.setCanceledOnTouchOutside(false);
        dialogProtocol.setCancelable(false);
        dialogProtocol.show();


        Window window = dialogProtocol.getWindow();
        assert window != null;
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置dialog在界面中的属性
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

重要的代码

如果发现宽度match_parent的dialog即使没有设置两边的margin也有margin,可以设置给dialog设置个background来解决

  Window window = dialogProtocol.getWindow();
        assert window != null;
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //设置dialog在界面中的属性
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值