【深入篇】Android常用布局方式简介

LinearLayout

线性布局是程序中最常见的布局方式。一般分为水平线性布局和竖直线性布局,通过android.orientation属性可以设置线性布局的方向。

在布局中操作颜色时,要用的是十六进制的数字来表示

例如:android:textColor="#0000FF"

   android:background="#0000FF"

Android:gravity

该属性用于控制布局中控件的对齐方式

如果是没有子控件的控件设置此属性,表示其内容的对齐方式

如果有子控件,表示其子控件的对齐方式

对齐方式可以重叠多个,以满足特殊的要求

例如:android:gravity="center_vertical|center_horizontal"

Android:Layout_weight

控制各个控件在布局中的相对大小,它的属性是一个非负的整数值

这里要注意的是,这个属性的分配方式是:

将所有在本布局下的非零值相加之后,分配给每个控件自己数值的百分比

AbsoluteLayout

绝对布局是指定子控件的xy精确坐标的布局方式。绝对布局缺乏灵活性,在没有绝对定位的情况下相比其他布局更加难以维护。

Android官方并不推荐使用。因为对不同屏幕大小或者分辨率的平板或者手机可能出现显示的问题。尤其在开发大众化软件时最好不要使用。

指定坐标时的属性(注意要用像素值表示坐标值)

android:layout_x="10px"

    android:layout_y="20px"

    android:background="#00FF00"

FrameLayout

所有添加到这个布局中的视图都以层叠的方式显示。第一个添加的组件放到最底层,最后添加的显示在最上面。

上一层的会覆盖下一层的组件。

android:layout_gravity属性,用来确定组件的位置

RelativeLayout

顾名思义,相对布局,在这个容器内部的子元素可以式样彼此之间的相对位置或者容器之间的相对位置来进行定位。

注意:不能在RelativeLayout容器与它的子元素之间产生循环依赖。

例如:不能将RelativeLayout的高设置成WRAP_CONTENT的时候将元素的高设置成ALIGN_PARENT_BOTTOM

相关布局属性:

Android:layout_above 将此控件置于给定ID控件之上

Android:layout_below 将此控件置于给定ID控件之下

Android:layout_toleftof 将此控件置于给定ID控件之左

Android:layout_torightof 将此控件置于给定ID控件之右

注意,这里的相对位置不可以覆盖。

相关对齐的属性:

android:layout_alignBaseline 该控件基线对齐给定ID控件的基线

android:layout_alignLeft 该控件于给定的ID控件左对齐

android:layout_alignTop该控件于给定的ID控件顶部对齐

android:layout_alignRight该控件于给定的ID控件右对齐

android:layout_alignBottom该控件于给定的ID控件底部对齐

对于容器的对齐方式:(在True的情况下)

android:layout_alignParentLeft 位于父控件的左部

android:layout_alignParentRight位于父控件的右部

android:layout_alignParentTop位于父控件的顶部

android:layout_alignParentBottom位于父控件的底部

对于容器位置关系的属性:(在True的情况下)

android:layout_centerInParent 控件被置于水平方向的中央

android:layout_centerHorizontal 控件被置于容器正中央

android:layout_centerVertical控件被置于垂直方向的中央

 

实例RelativeLayout

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:background="#0000FF"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:padding="10px"

    >

<TextView  

android:id="@+id/tv01"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="Type here:"

    />

<EditText  

android:id="@+id/txt01"

    android:layout_width="match_parent"

    android:layout_height="wrap_content" 

    android:layout_below="@id/tv01"

    

    />

    <Button

    android:id="@+id/btn01"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:layout_below="@id/txt01"

    android:layout_alignParentRight="true"

    android:text="OK"

    />

    <Button

    android:id="@+id/btn02"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:layout_below="@id/txt01"

    android:layout_toLeftOf="@id/btn01"

    android:text="Cancel"

    />

</RelativeLayout>

TableLayout

表格布局是一个ViewGruop以表格形式显示它的子视图(View)元素,即行和列标识一个视图的位置

这个布局方式同HTML中的表格布局非常类似,TableRow就像HTML表格的<tr>标记

常用属性:

android:collapseColumns 隐藏指定的列

如果要隐藏多列的话,就用逗号将列号隔开

android:stretchColumns尽量把指定的列填充空白部分

android:shrinkColumns 收缩指定的列以适应屏幕

android:layout_column 控件在TableRow中所处的列

android:layout_span 该控件所跨越的列数

                  ——结合网络资料,欢迎大家补充

原文地址:https://www.cnblogs.com/tbcxy/p/3242932.html