Android之六大布局

Android有常见的五大布局FrameLayoutTableLayoutLinearLayoutAbsoluteLayoutRelativeLayout,Android4.0之后新增了一个GridLayout布局。下面就针对每一种布局做详细解释。

1、帧布局(FrameLayout

帧布局就是简单的一层一层的往上叠加。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#0000ff"/>

     <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#00ff00"/>
</FrameLayout>

 

2、表格布局(TableLayout
顾名思义,表格布局就如同表格一样,有行有列。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="2"      //隐藏某列,(从0开始)
    android:shrinkColumns="1"        //伸缩某列
    android:stretchColumns="1" >     //延长列至填充剩余空间

    <TableRow>                       //定义一行

        <TextView                     //有几个组件就自动生成几列
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/username"
            android:text="@string/username" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/username" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/username"
            android:text="@string/username" />
    </TableRow>

</TableLayout>

 

3、线性布局(LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_weight="1">                 //设置权重,如果其他控件没有设置,那么此控件将占据剩余的空间

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:hint="@string/username" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
             android:hint="@string/username"/>
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="@string/login" />
<!-- layout_gravity:设置布局方向,相对于父控件    gravity:设置布局方向,相对于子控件 -->
</LinearLayout>

 

4、绝对布局(AbsoluteLayout
通过确定与源点的横纵距离确定组件的位置。Android的坐标源点是左上角,右为X的正方向,下为Y的正方向。

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/username"
        android:layout_x="20dp"
        android:layout_y="20dp"/>

    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/username"
        android:layout_x="150dp"
        android:layout_y="10dp"/>
</AbsoluteLayout>


5、相对布局(RelativeLayout
主要是通过id来定位同一布局文件中不同组件的位置

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textBase"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textleft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textBase"
        android:layout_toLeftOf="@id/textBase"     //在textBase左边
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textright"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textBase"  //与textBase下边对齐
        android:layout_toRightOf="@id/textBase"    //在textBase右边
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/texttop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textBase"
        android:layout_alignLeft="@id/textBase"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textbelow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/textBase"
        android:layout_below="@id/textBase"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textlefttop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/texttop"
        android:layout_toLeftOf="@id/texttop"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textrighttop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/texttop"
        android:layout_toRightOf="@id/texttop"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textleftbelow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textbelow"
        android:layout_toLeftOf="@id/textbelow"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textrightbelow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/textbelow"
        android:layout_toRightOf="@id/textbelow"
        android:background="@drawable/ic_launcher" />

</RelativeLayout>


6、网格布局(GridLayout

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="5"         //定义网格的列数
    android:rowCount="5" >          //定义网格的行数

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="5"    //占据5列
        android:layout_row="0"           //从第0列开始
        android:background="#ff0000"
        android:gravity="right"
        android:text="0"
        android:textSize="40dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"       //在第0列
        android:layout_row="1"          //在第1行
        android:text="7" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="1"
        android:text="8" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="1"
        android:text="9" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="1"
        android:text="/" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="4"
        android:layout_row="1"
        android:text="%" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="2"
        android:text="4" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="2"
        android:text="5" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="2"
        android:text="6" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="2"
        android:text="*" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="4"
        android:layout_row="2"
        android:text="1/x" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="3"
        android:text="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="3"
        android:text="2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="3"
        android:text="3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="3"
        android:text="-" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="4"
        android:layout_row="3"
        android:layout_rowSpan="2"
        android:layout_gravity="fill_vertical"   //在垂直方向上填充2行
        android:text="=" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="4"
        android:layout_columnSpan="2"
        android:layout_gravity="fill_horizontal"  //在水平方向上填充2列
        android:text="0" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="4"
        android:text="." />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="3"
        android:layout_row="4"
        android:text="+" />

</GridLayout>

 

总结:6种布局如上所示,一般都可以根据名称理解其作用。写布局问件时关键要细心,弄清楚组件具体摆放的位置,根据实际需求选择布局。而且,布局可以嵌套使用,使得组件的展现形式更加灵活多变!


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android 六宫格布局是指将界面分为六个等大小的方格,每个方格中可以放置不同的控件或者视图。这布局方式在Android应用的界面设计中经常使用,可以使界面看起来整齐、美观,并且提供了较高的灵活性。 实现六宫格布局的方法有很多,其中比较简单的方式是使用GridLayout布局管理器。GridLayout可以将子视图按照行和列的方式进行排列,因此非常适合用于六宫格的界面设计。 在XML布局文件中,我们可以通过设置GridLayout的属性来实现六宫格布局。首先,我们需要将GridLayout设置为6行1列,表示界面将被分为六个水平方向的等高行;然后,我们可以在每个格子中添加其他的控件或者视图。通过设置每个格子的权重、行列位置等属性,可以实现不同的布局效果,例如让某些格子占据更多的空间或者选择合适的控件来填充格子。 另外,我们还可以通过Java代码来实现六宫格布局。可以使用GridLayoutManager或者自定义布局管理器继承自RecyclerView.LayoutManager来实现。这方式可以更加灵活地控制子视图的排列方式,可以根据自己的需求定制不同的布局效果。 总之,Android六宫格布局是一常见且实用的界面布局方式,可以通过使用GridLayout或者自定义布局管理器来实现。这布局方式可以使界面整齐、美观,并且提供了较高的灵活性,适合用于不同类型的Android应用界面设计。 ### 回答2: 安卓的六宫格布局是一常见的应用界面布局方式,它将屏幕分割为2行3列的六个等大的格子,每个格子可以放置不同的应用模块或者功能模块。 此布局通常用于主屏幕或者应用程序的菜单界面,以提供快速访问和导航。每个格子可以自定义放置不同的应用图标、小部件或者快捷方式,以满足用户的个性化需求。 六宫格布局的优势在于简单直观,用户可以一目了然地找到和使用所需的应用或者功能。同时,由于每个格子的尺寸相同,不同的应用图标或者模块之间的界面一致性很高,提升了用户界面的美观度和易用性。 此外,六宫格布局还可以根据用户的喜好进行调整和定制。用户可以自由地拖动和排列格子的位置,以适应个人喜好和使用习惯。这灵活性使得用户可以根据自己的需求将常用的应用设置为更加方便的位置,提高了操作效率。 总的来说,安卓的六宫格布局提供了一简单直观且易于个性化的界面布局方式,使得用户可以快速访问和导航不同的应用或者功能模块。它为用户提供了良好的用户体验和操作效率,受到广大安卓用户的喜爱。 ### 回答3: 六宫格布局是一常见的Android布局方式,适用于需要将界面划分为6个等宽、等高的方格的情况。 在Android中,可以通过使用GridLayout布局管理器来实现六宫格布局。首先,在XML布局文件中定义一个GridLayout容器,并设置相关属性,如行数、列数、间距等。然后,在GridLayout中添加6个子视图,即代表六个方格的控件。 可以将六宫格布局分为两步骤:定义和设置属性与添加子视图。 在定义和设置属性方面,可以通过设置GridLayout的属性来实现六宫格布局的效果。比如,设置行数和列数为2,即可将布局分为2行3列的六个方格。可以使用layout_rowSpan和layout_columnSpan属性来设置某个子视图占据多个行或列的大小。也可以使用layout_gravity属性调整子视图在方格中的位置。 在添加子视图方面,可以使用GridLayout的addView方法来将子视图添加到布局中。可以使用LayoutInflater来实例化子视图,并为子视图设置相关属性。可以通过设置子视图的宽度和高度为0dp,以实现平均分配布局。 总结起来,通过使用GridLayout布局管理器,可实现六宫格布局,将界面划分为6个等宽、等高的方格。根据需要,可以通过设置各个子视图的属性和位置,来实现不同的布局效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值