美文网首页Android知识Android开发Android技术知识
UI布局初试three---四种基本布局

UI布局初试three---四种基本布局

作者: CodeSaid | 来源:发表于2016-12-11 15:30 被阅读189次
  • 版权声明:欢迎转载,转载请注明出处。 如果本文帮助到你,本人不胜荣幸,如果浪费了你的时间,本人深感抱歉。如果有什么错误,请一定指出,以免误导大家、也误导我。感谢关注。

什么是布局?

布局是一种可以放置很多控件的容器,他可以按照一定的规律调整内部控件的位置,从而编写出好看的界面。布局的内部也能够放置布局,形成多层嵌套。

线性布局 LinearLayout

LinearLayout 叫做线性布局,他会将其内部的控件在线性的方向依次排列。 他是按照属性android:orientation的指定,来决定其子视图设置为水平还是垂直。

  • horizontal 表示一行 (水平) 默认的值
  • vertical 表示一列 (垂直)

LinearLayout 是布局中的根视图,因此应将宽度和高度设置为 "match_parent",从而填满可供应用使用的整个屏幕区域。

栗子:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:layout_gravity="top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"/>
    </LinearLayout>

android:layout_gravity属性:指定控件在布局中对齐方式,需要根据父视图的排列方式来指定,比如父视图对齐方式为vertical,只有在水平方向上的对齐方式才有效

然后就是LinearLayout的重要属性---android:layout_weight:一般称为权重。他是使用比例的方式来指定控件的大小。

栗子:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Input someing"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button"/>
    </LinearLayout>                                                                                                                                                                                    

首先把width的值指定为0,可以理解成宽度不再受width控制,而由weight来控制,系统会把所有的weight的值加起来,然后按份分配给各个控件。另外,我们也可以只指定其中一部分控件的weight值来实现UI效果。

相对布局 RelativeLayout

他可以通过相对定位的方式让控件出现在布局的任何位置。可以相对与父视图定位,也可以相对于控件定位

栗子:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button 1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button2 " />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Button 3" />
        
    </RelativeLayout>

android:layout_alignParentLeft:相对于父控件左对齐,其他的依次类推
android:layout_centerInParent:与父控件的中心对齐

 栗子:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Button 3"/>
        
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/btn3"
            android:layout_toLeftOf="@id/btn3"
            android:text="Button 1" />
        
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/btn3"
            android:layout_toRightOf="@id/btn3"
            android:text="Button 2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/btn1"
            android:text="Button 4"/>
    </RelativeLayout>

android:layout_above:让一个控件位于其指定控件的上方,需要指定相对控件的id,其他的依次类推

android:layout_toLeftOf:让一个控件位于其指定控件的左侧

android:layout_alignLeft:让一个控件的左边缘与另外一个控件的左边缘对齐

这里我们需要注意的是:如果一个控件要引用另外一个控件的id时,该控件一定要定义在引用控件的后面,不然会找不到id。

帧布局 FrameLayout

FrameLayout是最简单的布局了。应用场景相对于前面两种来说也少了很多。 所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

栗子:
    <FrameLayout 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:textSize="50sp"
            android:textColor="#ff0000"
            android:text="Text"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </FrameLayout>

简而言之,他根本控制不了子控件在其内部的位置

百分比布局 Percent

可以不用match_parent或者wrap_content等方式来指定控件的大小, 而是直接指定控件在布局中所占的百分比,可以轻松实现按任意比例分配布局的效果。
LinearLayout已经可以使用权重来按比例分配控件大小了,所以只需要为FrameLayout和RelativeLayout提供扩展。

  • PercentFrameLayout 继承了FrameLayout的特性
  • PercentRelativeLayout 继承了RelativeLayout的特性

百分比布局属于新增的布局,我们要想使用他,就需要在项目下的build.gradle 文件添加百分比布局库的依赖。具体就是在dependencies闭包中添加以下内容:

compile 'com.android.support:percent:24.2.1'

栗子:
    <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:layout_gravity="left|top"
            android:text="Button 1"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="50%" />
    
        <Button
            android:layout_gravity="right|top"
            android:text="Button 2"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="50%" />
    
        <Button
            android:layout_gravity="left|bottom"
            android:text="Button 3"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="50%" />
    
        <Button
            android:layout_gravity="right|bottom"
            android:text="Button 4"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="50%" />
    
    </android.support.percent.PercentFrameLayout>

由于PercentFrameLayout不是系统内置的布局,所以我们需要写出完整的路径,并且给他定义一个app的命名空间。所以我们可以使用app:layout_widthPercent与app:layout_heightPercent来指定控件的宽与高

好了,对于四种基本布局的基本用法暂时就了解到这里。慢慢用到新的知识再来分享。

相关文章

  • UI布局初试three---四种基本布局

    版权声明:欢迎转载,转载请注明出处。 如果本文帮助到你,本人不胜荣幸,如果浪费了你的时间,本人深感抱歉。如果有什么...

  • 几种常见的css布局方式

    四种方式 element-ui布局容器(el-container) element-ui布局(el-row、el-...

  • UI布局初试---one

    (UI)用户界面 Android 应用的图形界面使用 View 对象和 ViewGroup 对象层次结构而构建。V...

  • CSS布局

    基本上,有四种可选的布局类型:1.固定宽度布局2.自适应布局3.弹性布局4.混合布局 1.固定布局以像素为单位,外...

  • iOS页面的布局方式

    iOS有三种基本的界面布局的方法,分别是手写UI,xib和storyboard。手写UI是最早进行UI界面布局的方...

  • Android UI的四种基本布局

    布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,从而编写出精美的界面。当然,布局的内部...

  • 布局

    今天学习了安卓的四种布局方式 线性布局 相对布局 帧布局 百分比布局

  • 仿网易新闻首页UI布局

    title : 仿网易新闻首页UI布局category : UI 仿网易新闻首页UI布局 标签(空格分隔): UI...

  • CSS2-3的盒模型

    Css2定义了四种布局模式 1) 块布局:呈现文档的布局模式 2) 行内布局:呈现文本的布局模式 3) 表格布局:...

  • Android零基础入门第30节:两分钟掌握FrameLayou

    前面学习了线性布局、相对布局、表格布局,那么本期来学习第四种布局——FrameLayout帧布局。 一、认识Fra...

网友评论

    本文标题:UI布局初试three---四种基本布局

    本文链接:https://www.haomeiwen.com/subject/cwrpmttx.html