Android Studio:相对布局 RelativeLayout

        在 Android 中,RelativeLayout 是一种布局,它允许你根据其他视图的位置来相对地定位视图。

        以下面的代码为例:

案例一:

 android:layout_alignParentTop="true": 将视图的顶部对齐到父容器的顶部。

android:layout_centerHorizontal="true": 将视图水平居中。

文本视图中的android:layout_below="@id/button1": 将视图放置在另一个视图(在此例中是 Button)的下方。



    
    

android:layout_alignParentBottom="true": 将视图的底部对齐父容器的底部。

android:layout_alignParentRight="true": 将视图的右侧对齐父容器的右侧。

android:layout_marginBottom="50dp"android:layout_marginRight="50dp": 为视图的底部和右侧添加间距。


    
  • android:layout_toLeftOf="@id/imageView1": 将视图放置在指定视图(imageView1)的左侧。
  • android:layout_alignTop="@id/imageView1": 将视图与指定视图(imageView1)的顶部对齐。

    

    
    

总体效果图如下:

Android Studio:相对布局 RelativeLayout_第1张图片

再看一个案例:

案例二:

先给出效果图

Android Studio:相对布局 RelativeLayout_第2张图片

完整代码如下:

线性布局作为根,垂直方向排列,android:padding="5dp"设置了内边距,也就是视图内容上下左右各压缩了5dp的显示区域。

线性布局内是5个相对布局组件,gravity="left|center" 具体含义是:内容会水平靠左,同时垂直居中。通过5个相对布局组件,可以实现内容的自由排列。



    

        

        
    

    

        

        
    
    
    

        

        
    
    
    

        

        
    
    
    

        
    
    
    

你可能感兴趣的:(android,studio,android,RelativeLayout)