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="標(biāo)題"
            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>




預(yù)覽顯示


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ǎn)擊
    }

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

運(yùn)行顯示效果双吆,可以看到magin失效了

image.png

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

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市歉提,隨后出現(xiàn)的幾起案子笛坦,更是在濱河造成了極大的恐慌,老刑警劉巖苔巨,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件版扩,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡侄泽,警方通過查閱死者的電腦和手機(jī)礁芦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悼尾,“玉大人柿扣,你說我怎么就攤上這事」胛海” “怎么了未状?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)析桥。 經(jīng)常有香客問我司草,道長(zhǎng),這世上最難降的妖魔是什么泡仗? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任埋虹,我火速辦了婚禮,結(jié)果婚禮上娩怎,老公的妹妹穿的比我還像新娘搔课。我一直安慰自己,他們只是感情好截亦,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布爬泥。 她就那樣靜靜地躺著旦事,像睡著了一般。 火紅的嫁衣襯著肌膚如雪急灭。 梳的紋絲不亂的頭發(fā)上姐浮,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音葬馋,去河邊找鬼卖鲤。 笑死,一個(gè)胖子當(dāng)著我的面吹牛畴嘶,可吹牛的內(nèi)容都是我干的蛋逾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼窗悯,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼区匣!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蒋院,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤亏钩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后欺旧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體姑丑,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年辞友,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了栅哀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡称龙,死狀恐怖留拾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鲫尊,我是刑警寧澤痴柔,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站马昨,受9級(jí)特大地震影響竞帽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鸿捧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望疙渣。 院中可真熱鬧匙奴,春花似錦、人聲如沸妄荔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至哗伯,卻和暖如春荒揣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背焊刹。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工系任, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人虐块。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓俩滥,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親贺奠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子霜旧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容