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èi)容\n內(nèi)容"
            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)//外部和返回鍵不可點擊
    }

調(diào)用的的是 dialog的setContentView(@NonNull View view) 方法

運行顯示效果,可以看到magin失效了

image.png

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

image.png
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容