美文网首页
Android UI 控件之六大布局

Android UI 控件之六大布局

作者: 凌云struggle | 来源:发表于2019-08-26 23:28 被阅读0次

      Android里的图形界面都是由View和ViewGroup以及他们的子类构成的: View:所有可视化控件的父类,提供组件描绘和时间处理方法 ViewGroup: View类的子类,可以拥有子控件,可以看作是容器 Android UI中的控件都是按照这种层次树的结构堆叠得,而创建UI布局的方式有两种, 自己在Java里写代码或者通过XML定义布局,后者显得更加方便和容易理解! 也是我们最常用的手段!另外我们一般很少直接用View和ViewGroup来写布局,更多的 时候使用它们的子类控件或容器来构建布局!

    1)fill_parent

    设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。

    2) wrap_content

    设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。

    3)match_parent

    Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了

    一、LinearLayout(线性布局)

    LinearLayout
    关于weight的详细用法参考菜鸟教程LinearLayout详解
    • 默认情况下,LinearLayout的布局为水平布局,使用orientation来改变布局方式,有vertical(竖直布局)和horizontal (水平布局)两种方式。

    divider分割线

    1. android:divider设置作为分割线的图片
    2. android:showDividers设置分割线的位置,none(无),beginning(开始),end(结束),middle(每两个组件间)
    3. dividerPadding设置分割线的Padding
       android:orientation="vertical"
        android:divider="@drawable/parting_line"
        android:showDividers="middle"
        android:dividerPadding="10dp"
    
    LinearLayout的简单例子:
    image.png
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入要保存的美文:"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="right">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="保存"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="清空"/>
        </LinearLayout>
    
    

    二、RelativeLayout(相对布局)

    核心属性图

    RelativeLayout
    • 其中,关于兄弟组件的小demo梅花布局
      梅花布局

    - margin与padding的区别

     &esnp;初学者对于这两个属性可能会有一点混淆,这里区分下: 首先margin代表的是偏移,比如marginleft = "5dp"表示组件离容器左边缘偏移5dp; 而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字 比如为TextView设置paddingleft = "5dp",则是在组件里的元素的左边填充5dp的空间! margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来!

    • margin可以设置为负数

    三、TableLayout(表格布局)

    介绍:

    相信学过HTML的朋友都知道,我们可以通过< table >< tr >< td >就可以生成一个HTML的表格, 而Android中也允许我们使用表格的方式来排列组件,就是行与列的方式,就说我们这节的TableLayout!

    ①如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!
    ②如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
    ③tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定
    ④tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!
    ⑤整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
    ⑥有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数

    android:collapseColumns:设置需要被隐藏的列的序号
    android:shrinkColumns:设置允许被收缩的列的列序号
    android:stretchColumns:设置运行被拉伸的列的列序号
    以上这三个属性的列号都是从0开始算的,

    四、FrameLayout(帧布局)

    • 前景图像:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。
      • android:foreground:*设置改帧布局容器的前景图像
      • android:foregroundGravity:设置前景图像显示的位置

    ps:get两个有意思的小项目

    • 随手指移动的萌妹子
    • 跑动的萌妹子

    五、GridLayout(网格布局)

    网格布局

    例子:计算机布局的实现


    image.png
    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".MainActivity"
        android:orientation="horizontal"
        android:columnCount="4"
        android:rowCount="6">
    
        <TextView
            android:layout_columnSpan="4"
            android:layout_gravity="fill"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@color/colorPrimary"
            android:text="0"
            android:textSize="50dp"/>
    
        <Button
            android:layout_columnSpan="2"
            android:layout_gravity="fill"
            android:text="回退" />
        <Button
            android:layout_columnSpan="2"
            android:layout_gravity="fill"
            android:text="清空" />
    
        <Button android:text="+"/>
        <Button android:text="1"/>
        <Button android:text="2"/>
        <Button android:text="3"/>
        <Button android:text="-"/>
        <Button android:text="4"/>
        <Button android:text="5"/>
        <Button android:text="6"/>
        <Button android:text="*"/>
        <Button android:text="7"/>
        <Button android:text="8"/>
        <Button android:text="9"/>
        <Button android:text="/"/>
        <Button
            android:layout_width="wrap_content"
            android:text="."/>
        <Button android:text="0"/>
        <Button android:text="="/>
    
    </GridLayout>
    

    六、AbsoluteLayout(绝对布局)

    前面已经介绍了,Android中的五大布局,在本节中会讲解第六个布局AbsoluteLayout(绝对布局), 之所以把这个放到最后,是因为绝对布局,我们基本上都是不会使用的,当然你也可以直接跳过这一 篇博文,不过作为一个喜欢增长姿势的程序员,我们还是可以了解这个AbsoluteLayout布局的, 相信大部分学过Java的都知道,我们在Java swing(不是spring哦)都用过这个绝对布局,但是Android 中我们用这个少的原因,就是因为我们开发的应用需要在很多的机型上面进行一个适配,如果你 使用了这个绝对布局的话,可能你在4寸的手机上是显示正常的,而换成5寸的手机,就可能出现偏移 和变形,所以的话,这个还是不建议使用了,当然,如果你不会写代码的话,又想玩玩android,那么写 布局的时候就可以通过ADT把需要的组件,拖拉到界面上!这个AbsoluteLayout是直接通过X,Y坐标来 控制组件在Activity中的位置的!另外这个但单位是dp!

    相关文章

      网友评论

          本文标题:Android UI 控件之六大布局

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