android布局方式—Linearlayout

线性布局是最常见的布局方式,它可以分为水平布局和垂直布局。

Android:layout_width宽度设置,有两种常见的值fill_parent填充父容器,wrap_content包裹内容

Android:layout_height设置高度,其值和layout_width一样

android:orientation来设置线性布局的方向

Android:gravity设置对齐方式,如果没有子控件则设置里面文字的对齐方式,如果有子控件则设置子控件的对齐方式,可以取三种值right,center,left,top,bottom

坐标的单位是px,高度和宽度的单位是dp

Android:background设置背景颜色

Android:layout_weight设置控件的相对大小,每个控件大小是在所有控件大小之和所占的比例,如果两个控件的layout_weight都为1,那么他们就各占50%。如果layout_weight为0,那么就是原始大小,其他控件的大小为除去这个控件后的总和所占的比例

Linearlayout可以嵌套使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> 
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"    
android:textColor="#00ff00"
android:text="@string/hello_android"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
> 
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"        
android:id="@+id/btn1"
        android:text="clickme1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"        
android:id="@+id/btn2"
        android:text="clickme2"
/>
</LinearLayout>
</LinearLayout

android布局方式—AbsolutelyLayout
absolutelylayout绝对布局,指定xy坐标,一般不推荐
android:layout_x指定x坐标
android:layout_y指定y坐标
<AbsolutelyLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
> 
<Button
android:layout_x="10px"
android:layout_y="10px"        
android:id="@+id/btn1"
        android:text="clickme1"
/>
</AbsolutelyLayout>

android布局方式—FrameLayout

FrameLayout层叠样式,所有添加的控件都叠加在一起,最先添加的再最下面,最后添加的再最上面。层叠的样式用gravity来设置

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
> 
<Button
android:layout_height="wrap_content"
android:layout_y="fill_parent"        
android:id="@+id/btn1"
android:text="click me1"
/>
<Button
android:layout_height="wrap_content"
android:layout_y="fill_parent"        
android:id="@+id/btn2"
android:text="click me1"
       android:gravity = “left”
/>
</FrameLayout>

android布局方式—RelativeLayout

相对布局,针对指定的控件来设置布局,有4个属性可以设置:android:layout_above当前控件在指定控件的上方,Android:layout_bellow当前控件在指定控件的下方,Android:layout_toLeftOf当前控件在指定控件的左边,Android:layout_toRightOf当前控件在指定控件的右边

布局的对齐方式有:Android:layout_alignBottom与指定控件底部对齐,Android:layout_alignTop与指定控件的顶部对齐,Android:layout_alignLeft与指定控件的左边对齐,Android:layout_alignRight与指定控件的右边对齐。

控件与父容器的对齐方式android:layout_alignParentLeft,如果为True,该控件位于容器的左侧;android:layout_alignParentRight,如果为True,该控件位于容器的右侧;android:layout_alignParentTop,如果为True,该控件位于容器的顶部;android:layout_alignParentBottom,如果为True,该控件位于容器的底部;android:layout_centerHorientatl,如果为True,该控件位于容器的水平中间;android:layout_centerVertical,如果为True,该控件位于容器的垂直中间;android:layout_centerInParent,如果为True,该控件位于容器的水平和垂直中间

< RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
> 
<Button
android:layout_height="wrap_content"
android:layout_y="fill_parent"        
android:id="@+id/btn1"
android:text="click me1"
/>
<Button
android:layout_height="wrap_content"
android:layout_y="fill_parent"        
android:id="@+id/btn2"
android:text="click me1"
       android:gravity = “left”
/>
</ RelativeLayout >

android布局方式—TableLayout

通过表格的形式来进行布局,和html一样。Tablerow表示行。Android:collapseColumns隐藏指定的列,从0号开始;Android:shrinkColumns收缩指定的列以适合屏幕,Android:strecthColumns尽量把指定的列填充空白;Android:layout_column控件在tablerow中所处的列;Android:layout_span该控件所跨越的列数

< TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Android:collapseColumns = “0,1” 隐藏第0列和第1列
> 
<Tablerow>
<Button
android:id="@+id/btn00"
android:text="click me1"
/>
<Button
android:id="@+id/btn01"
android:text="click me1"
/>
<Button
android:id="@+id/btn02"
android:text="click me1"
/>

    <Tablerow>
<Tablerow>
<Button
android:id="@+id/btn10"
android:text="click me1"
/>
<Button
android:id="@+id/btn12"
android:text="click me1"
Android:layout_column = “2” 放在第2行第2列
/>
    <Tablerow>
</ TableLayout >