1、如果LinearLayout中使用android:layout_marginRight不起作用,通過測試原來在android2.x中,如果一個控件中有android:layout_gravity屬性,就會出現android:layout_marginRight沒有應有的效果,而是把左邊距加到右邊距上去,直接去掉android:layout_gravity這個屬性就能解決
2、如果是在RelativeLayout中不起作用,請在這個代碼前加了一條android:layout_alignParentRight="true",就行(自己驗證成功)
3、如果是ScrollView中的LinearLayout 中設置margin 類的屬性無效,解決方法 LinearLayout中加android:layout_gravity="top" 屬性就ok,
4、如果是LineaerLayout放到scrollview或者RelativeLayou里面中layout_margin失效不起作用,解決方法在屬性里面加入android:layout_gravity="top",大家注意跟1是不一樣的,1是LinearLayout里面的控件,而這里是指LinearLayout在其他控件中的情況,請分清對待。
5.我在一個relativelayout中放置了一個relativelayout,結果這個relativelayout中的layout_marginTop一直不起作用,后來發現是因為我的最外層relativelayout增加了另一個android:gravity="center",導致了問題
上面不知道是是個bug還是什么其他原因。
以上內容轉載自:http://blog.csdn.net/lovexieyuan520/article/details/10499811
----我的經驗:
我希望在一個actionmode中添加一個布局,如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/navigation_bar" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RelativeLayout android:layout_marginTop="26dp" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_centerVertical="true" android:src="@drawable/fm_return_selector" /> <TextView android:id="@+id/select" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_toRightOf="@+id/cancel" android:layout_alignParentRight="true" android:textSize="13sp" android:textColor="#ffffff" /> <Button android:id="@+id/detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:layout_alignParentRight="true" android:src="@drawable/action_mode_detail" /> </RelativeLayout> </LinearLayout>
結果發現,text永遠不會居中,后來終於發現了問題,原來TORIGHTOF的意思是,你的控件左邊跟另一個控件的右邊對齊,如果你不添加layout_marginLeft,那么兩個之間的距離是為零的,也就是所謂的嚴格對齊。而且,他的優先級似乎比android:layout_alignParentRight="true"更高,這就是它不發生作用的原因,只要去掉TORIGHTOF這個屬性就行了,問題迎刃而解。