学习过Android的基本内容之后开始对Android的ui开发进行学习,主要是对布局的了解和对样式主题的应用。
Android的布局文件是XML的格式,而且该文件在res/layout文件夹中 。而布局文件可以用图形化的视图表示,在Graphical Layout中可以查看到。
Android的布局主要有以下几个类型:相对布局、线性布局、表格布局、网格布局、帧布局、绝对布局这几个方式。不同的布局会有不同的视觉效果。接下来简单学习下各个布局的设计代码:
相对布局的设计代码:(RelativeLayout)
<RelativeLayout xmlns:android="******/apk/res/android" //标签
xmlns:tools="******/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity'>
<Button //控件1
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" //顶部
android:layout_centerHorizontal="true" //居中
android:layout_marginTop="260dp"
//设置距离
android:text="Button1"/>
<Button //控件2
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1" //对齐
android:layout_marginBottom="100dp" //距离
android:layout_toRightOf="@+id/button1" //右边属性
android:text="Button2"/>
</RelativeLayout>
这样就定义了一个相对的布局,代码中的margin用于定义组件间的距离。android:margin用于定义控件四周的边距。而与margin类似的padding则是对内边距的设置,需要注意使用的范围和情况才可。在你设置控件和布局的时候需要注意使用“match_parent”或者是“wrap_content”的布局格式,需要注意避免将空间的宽度和高度设计为固定的值。
<LinearLayout xmlns:android="******/apk/res/android" //标签
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button //控件1
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button //控件2
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"/>
<Button //控件3
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
</LinearLayout>
<TableLayout xmlns:android="******/apk/res/android" //标签
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns=“2”>
<TableRow> //对象
<Button //控件1
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0" //第一列
android:text="Button1"/>
<Button //控件2
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Button2"/>
</TableRow>
<TableRow>
<Button //控件3
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Button3"/>
<Button //控件4
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Button4"/>
</TableRow><TableRow>
<Button //控件5
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="Button5"/>
</TableRow>
网格布局的设计代码:(GridLayout)
<GridrLayout xmlns:android="******/apk/res/android" //标签
android:layout_width="wrap_parent"
android:layout_height="wrap_parent"
android:gravity=“center”
android:columnCount="4"
android:orientation="horizontal">
<Button
android:layout_column="3"
android:text="/"/>
<Button android:text="1"/>
<Button android:text="2"/>
<Button android:text="3"/>
<Button android:text="4"/>
<Button android:text="5"/>
<Button android:text="6"/>
<Button android:text="7"/>
<Button android:text="8"/>
<Button android:text="9"/>
<Button android:layout_gravity="fill"
androoid:alyout_rowSpan="3" //占用3行
android:text="+"/>
<Button
android:layout_columnSpan="2" //占用2列
android:layout_gravity="fill"
android:text="0" />
<button android:text="00" />
<Button
android:layout_columnSpan="3" //占用3列
android:layout_gravity="fill"
android:text="=" />
</GridLayout>
这个网格布局是Android4.0新增的布局,用一组无限细的直线将绘图区域分成行列和单元。android:layout_column表示按钮在第几列,android:layout_rowSpan表示控件占用几行,android:layout——columnSpan表示控件占几列。
帧布局的设计代码:(FrameLayout)
<FrameLayout xmlns:android="******/apk/res/android" //标签
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button //控件1
android:id="@+id/button1"
android:layout_width="294dp"
android:layout_height="294dp"
android:text="Button1"/>
<Button //控件2
android:id="@+id/button2"
android:layout_width="218dp"
android:layout_height="214dp"
android:text="Button2"/>
<Button //控件3
android:id="@+id/button3"
android:layout_width="156dp"
android:layout_height="143dp"
android:text="Button3"/>
</FrameLayout>
帧布局的设计是为每一个加入其中的控件创建一个空白的区域,称之为帧。如果添加多个控件则会按照顺序在屏幕的左上角进行显示,并且会透明显示之前的看控件文本。常见的刮刮卡就是通过帧布局来实现的!
绝对布局的设计代码:(AbsoluteLayout)
<AbsoluteLayout xmlns:android="******/apk/res/android" //标签
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="50dp"
android:text="Button1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="150dp"
android:text="Button2"/>
</AbsoluteLayout>绝对布局的设计形式主要在于通过指定x,y的坐标来控制每一个控件的具体位置,实际工程中不提倡这个布局。