Android: RelativeLayout 记录

1、 基本内容

    一图解决:

                      Android: RelativeLayout 记录


2、父容器定位示意图

                    Android: RelativeLayout 记录


3、根据兄弟组件定位

                     Android: RelativeLayout 记录

    注意,此图组件1和2是兄弟组件,组件3不是。兄弟组件指是同一层次容器的组件。梅花布局比较有趣,也比较简单。大家也可以试试。


4、margin 和 padding 的区别

   margin代表的是偏移,比如marginleft = "5dp"表示组件离容器左边缘偏移5dp;padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字,比如为TextView设置paddingleft = "5dp",则是在组件里的元素的左边填充5dp的空间! margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来!这个需要代码理解一下。

<Button    
        android:id="@+id/btn1"     
        android:layout_height="wrap_content"    
        android:layout_width="wrap_content"    
        android:text="Button"/>    
    <Button    
        android:paddingLeft="100dp"     
        android:layout_height="wrap_content"    
        android:layout_width="wrap_content"    
        android:text="Button"    
        android:layout_toRightOf="@id/btn1"/>    
        
    <Button    
        android:id="@+id/btn2"     
        android:layout_height="wrap_content"    
        android:layout_width="wrap_content"    
        android:text="Button"    
        android:layout_alignParentBottom="true"/>    
    <Button    
        android:layout_marginLeft="100dp"     
        android:layout_height="wrap_content"    
        android:layout_width="wrap_content"    
        android:text="Button"    
        android:layout_toRightOf="@id/btn2"     
        android:layout_alignParentBottom="true"/>    

以上代码得到的结果为

                                  Android: RelativeLayout 记录


5、margin 更有趣常见的一点

    margin可以设置为负数,比如一些小窗体上的关闭按钮,就需要设置为负数,这样就会和小窗体结合在一起。

    这个也很容易理解,就比如直角坐标系一样,有正向反向。