六大布局

安卓六大布局

1、线性布局(LinearLayout):按照垂直或者水平方向布局的组件
2、帧布局(FrameLayout):组件从屏幕左上方布局组件
3、表格布局(TableLayout):按照行列方式布局组件
4、绝对布局(AbsoluteLayout):按照绝对坐标来布局组件
5、相对布局(RelativeLayout):相对其它组件的布局方式
6、约束布局 (ConstraintLayout):按照约束布局组件

线性布局

img

线性布局效果图

<?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="250dp"        
    android:orientation="horizontal">        
    <TextView           
        android:layout_width="96dp"      
        android:layout_height="match_parent" 
        android:background="#b2dfdb" />        
    <TextView            
        android:layout_width="96dp"               
        android:layout_height="match_parent"             
        android:background="#80cbc4" />       
     <TextView            
        android:layout_width="96dp"                   
        android:layout_height="match_parent"            
        android:background="#4db6ac" />        
    <TextView            
        android:layout_width="96dp"            
        android:layout_height="match_parent"            
        android:background="#26a69a" />    
</LinearLayout>
    
<LinearLayout        
    android:layout_width="match_parent"            
    android:layout_height="match_parent"        
    android:orientation="vertical">        
    <TextView           
        android:layout_width="match_parent"            
        android:layout_height="68dp"            
        android:background="#b2dfdb" />        
    <TextView            
        android:layout_width="match_parent"                
        android:layout_height="68dp"            
        android:background="#80cbc4" />        
    <TextView            
        android:layout_width="match_parent"                
        android:layout_height="68dp"            
        android:background="#4db6ac" />        
    <TextView            
        android:layout_width="match_parent"                    
        android:layout_height="68dp"            
        android:background="#26a69a" />    
</LinearLayout>

</LinearLayout>

线性布局特点,同一级组件之间没有互相依赖,按照添加顺序依次排列,其中线性布局最重要的属性是android:orientation,通过该属性可以指定水平方向排列还是纵向排列

帧布局

img

帧布局效果图

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="#FF6143" />

    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:background="#7BFE00" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="#FFFF00" />

</FrameLayout>

帧布局会按照添加顺序层叠在一起,默认层叠在左上角位置,本例子因为设置了layout_gravity=“center”,所以层叠在视图中心位置

表格布局

img

表格视图效果图

<?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="wrap_content"
    android:background="#ff0000"
    android:shrinkColumns="0,2"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我占据一行" />

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="111111111111111111111111" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="222222222222222222222222" >
        </Button>
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="000000000000000000000000" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="11" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="222222222222222222222222" >
        </Button>
    </TableRow>

    <TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="000000000000000000000000" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="我占据2列" >
        </Button>
    </TableRow>
</TableLayout>

TableLayout继承LinearLayout,有元素单独占一行可以不写TableRow
TableLayout的一些属性:

  • android:shrinkColumns 设置可收缩的列
  • android:stretchColumns 设置可伸展的列
  • android:collapseColumns 设置要隐藏的列
    默认所有的列为stretchColumns,如只有一个控件则独占一行
    每列宽度的计算原则:首先根据可伸展列根据其内容最长的一行占据一行的百分比,再根据可收缩的列等分剩余的部分
    例如图中,优先计算“ 111111111111111111111111”的宽度,再由剩下的列等分剩余的空间
    如果遇到空间已经不足了,则剩余的列均不显示
    控件的一些属性:
    android:layout_column表示当前控件在第几列
    android:layout_span表示合并单元格个数

绝对布局

难以实现多分辨率适配,不建议使用

相对布局

已使用约束布局替代

约束布局

img

约束布局效果图

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="验证码"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="手机号"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="197dp"
        android:layout_height="39dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="请输入验证码"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="298dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="请输入手机号"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="获取验证码"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="358dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="开始"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />
</android.support.constraint.ConstraintLayout>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值