android dialog布局文件中根布局layout_margin不生效的问题

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="39dp"
        android:layout_marginRight="39dp"
        android:background="@drawable/shape_solidffffff_corners8">


        <TextView
            android:id="@+id/txt_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text="标题"
            android:textColor="#141414"
            android:textSize="17sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/txt_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txt_title"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="28dp"
            android:layout_marginRight="30dp"
            android:layout_marginBottom="30dp"
            android:gravity="left"
            android:lineSpacingExtra="4dp"
            android:text="内容\n内容"
            android:textColor="#555555"
            android:textSize="15sp"
            app:layout_constraintBottom_toTopOf="@+id/txt_confirm"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txt_title" />


        <TextView
            android:id="@+id/txt_cancel"
            android:layout_width="0dp"
            android:layout_height="38dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="20dp"
            android:layout_weight="1"
            android:background="@drawable/shape_solid141414_corners_round"
            android:gravity="center"
            android:text="@string/label_cancel"
            android:textColor="@color/white"
            android:textSize="13sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/txt_confirm" />

        <TextView
            android:id="@+id/txt_confirm"
            android:layout_width="0dp"
            android:layout_height="38dp"
            android:layout_below="@+id/txt_content"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="20dp"
            android:layout_weight="1"
            android:background="@drawable/shape_solidffb533_corners_round"
            android:gravity="center"
            android:text="@string/label_conform1"
            android:textColor="@color/white"
            android:textSize="13sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/txt_cancel"
            app:layout_constraintRight_toRightOf="parent" />


        <ImageView
            android:id="@+id/close"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginTop="18dp"
            android:layout_marginRight="12dp"
            android:src="@mipmap/close"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </android.support.constraint.ConstraintLayout>




预览显示


image.png

在根布局中加入了marginLeft和marginRight
dialog 初始化部分代码

 init {
        val root = layoutInflater.inflate(builder.layoutRes ?: R.layout.dialog_tips, null)
        setContentView(root)
        setContentView(builder.layoutRes ?: R.layout.dialog_tips)
        val params = window.attributes
        params.gravity = builder.gravity
        params.width = ViewGroup.LayoutParams.MATCH_PARENT
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT
        window.attributes = params
        builder.animStyle?.apply { window.setWindowAnimations(builder.animStyle!!) }
        builder.bindListener?.apply {
            builder.bindListener(this@CommonDialog, window.decorView)
        }
        builder.bind?.apply {
            bind(this@CommonDialog, window.decorView)
        }
        setCancelable(builder.cancelable)//外部和返回键不可点击
    }

调用的的是 dialog的setContentView(@NonNull View view) 方法

运行显示效果,可以看到magin失效了

image.png

改成 dialog的setContentView(@LayoutRes int layoutResID)后显示正常

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

推荐阅读更多精彩内容