安卓学习(2)

 今天学习了安卓比较重要的两个布局:

(1)线性布局

 线性布局:线性布局是我们在开发中最常见的布局方式之一,线性布局可以分为水平线性布局和垂直线性布局这两种布局方式

(2)相对布局

  相对布局:相对布局也是常用的布局之一,它可以设置某一个控件相对于其他控件的位置,这些位置可以包括上下左右等,因此相较于其他的布局方式而言具有很大的灵活性。

 

 

 

通过查阅相关资料了解了两个布局的详解和区别:

        ①线性布局的属性(决定布局中元素的位置和布局):

android:layout_gravity ( 是本元素相对于父元素的对齐方式 )

android:gravity="bottom|right"(是本元素所有子元素的对齐方式,设置在父元素上,多个值用|隔开)

android:layout_gravity (子元素在父元素的对齐方式,设置在子元素上)

当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left(左),right(右),center_horizontal(水平居中) 是生效的。
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top(上),bottom(下),center_vertical (垂直居中)是生效的。

android:padding="10dp" (是本元素所有子元素的与父元素边缘的距离,设置在父元素上) 

android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上)

android:orientation (线性布局以列或行来显示内部子元素)

android:layout_weight ="1"  分配分配权重值

          ②相对布局常用的属性:

第一类:属性值为true或false
  android:layout_centerHrizontal 水平居中
  android:layout_centerVertical 垂直居中
  android:layout_centerInparent 相对于父元素完全居中
  android:layout_alignParentBottom 贴紧父元素的下边缘
  android:layout_alignParentLeft 贴紧父元素的左边缘
  android:layout_alignParentRight 贴紧父元素的右边缘
  android:layout_alignParentTop 贴紧父元素的上边缘
  android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
  第二类:属性值必须为id的引用名“@id/id-name”
  android:layout_below 在某元素的下方
  android:layout_above 在某元素的的上方
  android:layout_toLeftOf 在某元素的左边
  android:layout_toRightOf 在某元素的右边
  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
  第三类:属性值为具体的像素值,如30dip,40px
  android:layout_marginBottom 离某元素底边缘的距离
  android:layout_marginLeft 离某元素左边缘的距离
  android:layout_marginRight 离某元素右边缘的距离
  android:layout_marginTop 离某元素上边缘的距离

 区别:

线性布局(LinearLayout):在该标签下的所有子元素会根据orientation属性的值来决定是按行或者是按列来逐个显示,而相对布局,则是根据控件的相对位置而言,比如居于按钮的左侧或者右侧,所以说,线性布局比较适合所有控件都是整齐排列的页面,相对布局比较随意一点,可以按照自己的想法来放置控件的位置。但是相对布局的写起来比较麻烦一点,需要自己考虑好所有控件的的布局。

 

 

线性布局: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

 2     android:orientation="vertical"   
 3     android:layout_width="fill_parent"  
 4     android:layout_height="fill_parent" >  
 5     
 6     <Button  
 7         android:id="@+id/button1"  
 8         android:layout_width="wrap_content"  
 9         android:layout_height="wrap_content"  
10         android:text="@string/app_name" />  
11    
12     <Button  
13         android:id="@+id/button2"  
14         android:layout_width="wrap_content"  
15         android:layout_height="wrap_content"  
16         android:text="@string/hello_world" />  
17    
18     <Button  
19         android:id="@+id/button3"  
20         android:layout_width="wrap_content"  
21         android:layout_height="wrap_content"  
22         android:text="@string/test" />  
23    
24 </LinearLayout>

 

 

相对布局:

 1  <RelativeLayout   
 2         android:layout_width="fill_parent"  
 3         android:layout_height="fill_parent">  
 4         <Button  
 5             android:id="@+id/button2"  
 6             android:layout_width="wrap_content"  
 7             android:layout_height="wrap_content"  
 8             android:layout_toRightOf="@id/button1"  
 9             android:layout_alignTop="@id/button1"  
10             android:text="@string/hello_world" />  
11    
12         <Button  
13             android:id="@+id/button1"  
14             android:layout_width="wrap_content"  
15             android:layout_height="wrap_content"  
16             android:layout_alignParentLeft="true"  
17             android:layout_alignParentTop="true"  
18             android:text="@string/app_name" />  
19    
20     </RelativeLayout>

 

posted on 2020-02-02 19:53  Mrsen  阅读(146)  评论(0编辑  收藏  举报

导航