alertDialog设置自定义View时,两侧有空白

练习代码,请无视命名规则等细节, ̄□ ̄||

使用alertDialog自定义view代码:

 View view = View.inflate(getApplicationContext(),R.layout.dialog_biaozhi,null);
        TextView tvShuoMing = view.findViewById(R.id.tv_shuoming);
        ImageView imageView = view.findViewById(R.id.img_cuowu);

        tvShuoMing.setText(datas.get(pos).getDescription());

        final AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setView(view)
                .create();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();

R.layout.dialog_biaozhi的代码:

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

    android:layout_height="wrap_content">


    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/colorWhite"
        android:orientation="vertical">

        <RelativeLayout
            android:background="@color/colorMain"
            android:layout_width="match_parent"
            android:layout_height="35dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="说明"
                android:textColor="@color/colorWhite"
                android:textSize="16sp" />


            <ImageView
                android:id="@+id/img_cuowu"
                android:src="@mipmap/img_cuowu"
                android:layout_marginRight="20px"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_width="20dp"
                android:layout_height="20dp" />

        </RelativeLayout>


        <TextView

            android:id="@+id/tv_shuoming"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="40dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:lineSpacingMultiplier="1.4"
            android:text="表示禁止装载高度超过标志所示数值的车辆通行。表示禁止装载高度超过标志所示数值的车辆通行。表示禁止装载高度超过标志所示数值的车辆通行。" />

    </RelativeLayout>

</RelativeLayout>

布局效果右侧:

 顶部是蓝色的标题栏,但是实际上显示的时候,蓝色的标题栏两侧留有空白,但是我的代码是设置为了match_parent的,尝试过设置背景色为透明等方法无果,还是下图这样的情况:

两侧留有空白,百思不得其解后,终于找到了解决方法:

在alerDialog.show()方法后加上这句:

完美解决:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值