轉(zhuǎn)載請(qǐng)以鏈接形式標(biāo)明出處: http://www.reibang.com/p/3924ab03a97b
本文出自:103style的博客
android:layout_marginEnd隱藏的坑仰担,巨坑
類似問(wèn)題 setMargins無(wú)效
相信稍微有強(qiáng)迫癥的開(kāi)發(fā)小伙伴都會(huì)看到xml中的類似的這種warning提示
“Consider addingandroid:layout_marginEnd="@dimen/px_30_w750" to better support right-to-left layouts less... ”
在你寫(xiě)了左邊距和右邊距不相等的時(shí)候,就會(huì)提示你
然而這種平時(shí)是不會(huì)有什么問(wèn)題的缨睡!
當(dāng)你需要 動(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í)寫(xiě)了 android:layout_marginEnd 和 android:layout_marginRight , 他會(huì)去讀 android:layout_marginEnd....
然后 你設(shè)置的setMargins 就起不了作用了...
實(shí)際效果是這樣的
具體 android:layout_marginEnd 和 android:layout_marginRight 在布局的時(shí)候怎么添加的源碼 我就先不研究了,后面有時(shí)間再補(bǔ)上
需要了解的可以自行看看
轉(zhuǎn)載請(qǐng)以鏈接形式標(biāo)明出處:http://www.reibang.com/p/3924ab03a97b
本文出自:103style