LinearLayout-margin不起作用的处理

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这个属性就行了,问题迎刃而解。

原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_align_150315232.html