android:layout_marginEnd隱藏的坑 or setMargins無效

轉(zhuǎn)載請以鏈接形式標(biāo)明出處: http://www.itdecent.cn/p/3924ab03a97b
本文出自:103style的博客

android:layout_marginEnd隱藏的坑,巨坑
類似問題 setMargins無效

相信稍微有強(qiáng)迫癥的開發(fā)小伙伴都會看到xml中的類似的這種warning提示

“Consider addingandroid:layout_marginEnd="@dimen/px_30_w750" to better support right-to-left layouts less... ”

在你寫了左邊距和右邊距不相等的時(shí)候,就會提示你

然而這種平時(shí)是不會有什么問題的!
當(dāng)你需要 動態(tài)改變 控件位置的時(shí)候,
比如這樣,

 if (test != null) {
        RelativeLayout.LayoutParams testLP = (RelativeLayout.LayoutParams) test.getLayoutParams();
        testLP .setMargins(0, 0,
                DensityUtil.getSize(landsreen ? R.dimen.px_130_w750 : R.dimen.px_30_w750),
                DensityUtil.getSize(landsreen ? R.dimen.px_140_w750 : R.dimen.px_316_w750));
        test.setLayoutParams(testLP );
    }

然而setMargins的源碼改變的是rightMargin
setMarginEnd的源碼改變的才是endMargin

 public void setMargins(int left, int top, int right, int bottom) {
        leftMargin = left;
        topMargin = top;
        rightMargin = right;
        bottomMargin = bottom;
        mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK;
        mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK;
        if (isMarginRelative()) {
            mMarginFlags |= NEED_RESOLUTION_MASK;
        } else {
            mMarginFlags &= ~NEED_RESOLUTION_MASK;
        }
    }


 public void setMarginEnd(int end) {
        endMargin = end;
        mMarginFlags |= NEED_RESOLUTION_MASK;
    }

然后在API LEVEL 17的時(shí)候 如果你同時(shí)寫了 android:layout_marginEnd 和 android:layout_marginRight , 他會去讀 android:layout_marginEnd....

然后 你設(shè)置的setMargins 就起不了作用了...

實(shí)際效果是這樣的

具體 android:layout_marginEnd 和 android:layout_marginRight 在布局的時(shí)候怎么添加的源碼 我就先不研究了,后面有時(shí)間再補(bǔ)上
需要了解的可以自行看看

轉(zhuǎn)載請以鏈接形式標(biāo)明出處:http://www.itdecent.cn/p/3924ab03a97b
本文出自:103style

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

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

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