四、Android绝对布局

4.1布局的类型
4.1.1线性布局(Linear Layout)
线性布局是Android中较为常用的布局方式,它使用标签表示。
线性布局有两种方式指定控件位置,一种是水平方向,一种是竖直方向。
标签属性:相当于html css样式属性; android:开头
LinearLayout详解:
1.常用属性

①orientation: 布局中组件的排列方式,有horizontal(水平,vertical(垂直,默认),两种方式。
②gravity:控制组件所包含的子元素的对齐方式。
③layout grovity:控制该组件在父容器里的对其方式
④layout width: 布局的宽度,通常不直接写数字的,用wrap content(组件实际大小),fill-parent或match-parent填满父容器。
⑤Hlyout hight:布局的高度,参数同上。
⑥ld: 为该组件设置一个资源id.在java文件中可以通过findViewByld(id)找到该组件 。
⑦backgreund: 为该组件设置一个背景图片,或者直接用颜色覆盖。

2.weight(权重):该属性用来等比例的划分区域
最简单的用法:要等比例划分,分谁,谁为0,weight按比例即可。
weight使用详解:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        android:text="按钮一"
        android:layout_weight="0"/>
 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:text="按钮二"
        android:layout_weight="1"/>
</LinearLayout>

3.divider分割线
该属性用于为Linear Layout设置分割线图片,通过showDividers来设置分割线 的所在位置.有四个可选值none,middle,begining,end;还可以通过:
①divider:为Linearlyouti限分割线的图片。
②dividerPadding:设置分割线padding。

4.1.2相对布局(Relative Layout)
在Eclipse中开发Android程序时,默认采用的就是相对布局。
相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。
:①页面设计复杂时建议使用此布局
②添加一个组件默认:左上角对齐
android:Layout_aligin parentLeft=“true”
Relative Layout详解:
在这里插入图片描述代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent" >    
    
    <!-- 在容器中间的 -->    
    <ImageView    
        android:id="@+id/img1"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_centerInParent="true"    
        android:src="@drawable/pic1"/>    
        
    <!-- 在中间图片的左边 -->  
    <ImageView    
        android:id="@+id/img2"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_toLeftOf="@id/img1"    
        android:layout_centerVertical="true"    
        android:src="@drawable/pic2"/>    
        
    <!-- 在中间图片的右边 -->  
    <ImageView    
        android:id="@+id/img3"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_toRightOf="@id/img1"    
        android:layout_centerVertical="true"    
        android:src="@drawable/pic3"/>    
        
    <!-- 在中间图片的上面-->    
    <ImageView    
        android:id="@+id/img4"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_above="@id/img1"    
        android:layout_centerHorizontal="true"    
        android:src="@drawable/pic4"/>    
        
    <!-- 在中间图片的下面 -->    
    <ImageView    
        android:id="@+id/img5"     
        android:layout_width="80dp"    
        android:layout_height="80dp"    
        android:layout_below="@id/img1"    
        android:layout_centerHorizontal="true"    
        android:src="@drawable/pic5"/>    
    
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/weixin_45802395/article/details/113443608