Android开发之玩转FlexboxLayout布局

摘要:
1.当然,它是在项目引入之前引入的。Google已在GitHub上打开此控件。地址如下:https://github.com/google/flexbox-layoutOK,项目中介绍的方法如下:在模块的gradle文件中添加以下行:compile‘com。谷歌。android:flexbox:0.2.3'版本号是本文发布时的最新版本号。

在这之前,我曾认真的研究过鸿洋大神的Android 自定义ViewGroup 实战篇 -> 实现FlowLayout,按照大神的思路写出了一个流式布局,所有的东西都是难者不会会者不难,当自己能自定义流式布局的时候就会觉得这东西原来很简单了。如果各位小伙伴也看过那篇文章的话,应该知道自定义流式布局还是非常麻烦的,不过Google今年开源了新的容器,就是这个FlexboxLayout,如果你玩过前端开发或者玩过RN,就会觉得这个FlexboxLayout真是简单,OK,那我们今天就来看看这个FlexboxLayout的使用吧!先来看看显示效果:

Android开发之玩转FlexboxLayout布局第1张

OK,我们来看看这个东东要怎么实现吧!

1.引入项目

使用之前当然是先引入了,Google在GitHub上开源了这个控件,地址如下:

                 https://github.com/google/flexbox-layout

OK,在项目中引入方式如下:

在module的gradle文件中添加如下一行:

compile 'com.google.android:flexbox:0.2.3'

版本号为本文发布时的最新版本号。

引入之后就可以使用了。

2.基本用法

根据GitHub上给我们的Demo,可以看到FlexboxLayout在使用的过程中只需要用容器将我们的子控件包裹起来就行了,主布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="org.lenve.flexbox.MainActivity">

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:flexWrap="wrap">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="1.水陆草木之花"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="2.可爱者甚蕃"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="3.晋陶渊明独爱菊"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="4.自李唐来"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="5.世人甚爱牡丹"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="6.予独爱莲之出淤泥而不染"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="7.濯清涟而不妖"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="8.中通外直"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="9.不蔓不枝"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="10.香远益清"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/tv_bg"
            android:padding="8dp"
            android:text="11.亭亭净植"/>
    </com.google.android.flexbox.FlexboxLayout>
</RelativeLayout>

显示效果就是我们上文贴出来的图。3.父容器属性简介

flexWrap    属性表示换行与否,默认为noWrap,表示不换行,wrap表示自动换行,还有一个wrap_reverse 表示副轴反转,副轴的含义我们一会来说。

flexDirection  表示子元素的排列方向,元素的排列方向为主轴的方向,该属性有四种取值,不同取值对应不同的主副轴,看下面一张图:

Android开发之玩转FlexboxLayout布局第2张


默认为row,所以如果我给flexWrap取wrap_reverse属性,则效果如下:

Android开发之玩转FlexboxLayout布局第3张

副轴取反,由于flexDirection默认为row,即副轴为竖直方向,副轴取反,即竖直方向倒着显示。同理,如果我给flexDirection属性设置为column,对应主轴方向为竖直向下,这个时候控件就会按如下方式来显示:

Android开发之玩转FlexboxLayout布局第4张

其它值我就不一一测试了。

justifyContent 表示控件沿主轴对齐方向,有五种取值,默认情况下大家看到控件是左对齐(flex_start),另外还有主轴居中对齐(center):

Android开发之玩转FlexboxLayout布局第5张

主轴居右对齐(flex_end):

Android开发之玩转FlexboxLayout布局第6张

两端对齐,子元素之间的间隔相等,但是两端的子元素分别和左右两边的间距为0(space_between):

Android开发之玩转FlexboxLayout布局第7张

子元素两端的距离相等,所有子元素两端的距离都相相等(space_around):

Android开发之玩转FlexboxLayout布局第8张

alignContent 表示控件在副轴上的对齐方向(针对多行元素),默认值为stretch,表示占满整个副轴,因为上文中我把FlexboxLayout的高度设置为包裹内容,所以这个属性大家可能没看到效果,这里我把FlexboxLayout的高度改为match_parent,我们再来看看效果:

代码:

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:alignContent="stretch"
        app:flexWrap="wrap">
		....
		....

效果:

Android开发之玩转FlexboxLayout布局第9张

大家看到系统会自动放大子元素的高度以使之填满父容器。

与副轴起点对齐(flex_start):

代码:

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:alignContent="flex_start"
        app:flexWrap="wrap">
....
....

Android开发之玩转FlexboxLayout布局第10张

与副轴终点对齐(flex_end):

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:alignContent="flex_end"
        app:flexWrap="wrap">
...
...

Android开发之玩转FlexboxLayout布局第11张

还有两个值,分别是space_around和space_between,意思和上文说的一样,这里不再赘述。

alignItems 也是描述元素在副轴上的对齐方向(针对单行),属性含义和上文基本一致,只是多了一个baseline,表示基线对齐,其余属性不赘述。


这里说的都是父容器的属性,那么子元素都有哪些属性呢?

4.子元素属性简介

app:layout_order="2"
这个表示子元素的优先级,默认值为1,数值越大越靠后显示。


app:layout_flexGrow="2"

这个类似于权重属性,来个示例代码:

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:background="@color/colorPrimary"
            app:layout_flexGrow="2"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:background="@color/colorAccent"
            app:layout_flexGrow="1"/>
    </com.google.android.flexbox.FlexboxLayout>

显示效果:

Android开发之玩转FlexboxLayout布局第12张


app:layout_flexShrink="2"

表示空间不足时子控件的缩放比例,0表示不缩放,比如下面一行代码:

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="300dp"
            android:layout_height="48dp"
            app:layout_flexShrink="2"
            android:background="@color/colorPrimary"/>

        <TextView
            app:layout_flexShrink="1"
            android:layout_width="100dp"
            android:layout_height="48dp"
            android:background="@color/colorAccent"/>
    </com.google.android.flexbox.FlexboxLayout>

父容器总宽度为300dp,结果两个子元素加起来就400,超过了100dp,总共需要缩小100dp,根据flexShrink属性,第一个TextView缩小100的三分之二,第二个TextView缩小100的三分之一。


Ok,以上就是FlexboxLayout的一个基本使用,更多资料请参考https://github.com/google/flexbox-layout


以上。



免责声明:文章转载自《Android开发之玩转FlexboxLayout布局》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇gridview中的日期总是显示到时分秒,为了让只其显示年月日,采用如下代码:缓存cache下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

css设置文字中间的小竖线

主要css属性是border-right border-right:1px solid gray; padding-right:10px; padding-left:10px; <div data-bind="foreach:RequestListAll"> <a data-bind="text:PositionName...

QTableWidget详解(样式、右键菜单、表头塌陷、多选等)(转)

在Qt的开发过程中,时常会用到表单(QTableWidget)这个控件,网上的资料不少,但是都是最基本的,有一些比较经常遇到的问题也说得不太清楚。所以,今天就在这里总结一下! 以下为个人模拟Windows资源管理器的一个表单 一、设置表单样式 table_widget->setColumnCount(4); //设置列数 table_widget-&...

Android上GDB的使用

转自:blog.163.com/bjtornado@yeah/blog/static/69510484201081181657449/ 1) 编译包含Debug 信息设置如下环境变量:TARGET_BUILD_TYPE=debugTARGET_STRIP_MODULE=false (发现即使设置编译为Debug模式, 最后生成的文件系统中的程序和库还是会被...

Android实现程序前后台切换效果

太久没动这一块了。借助了下面的文章得到了类似QQ的效果 ,是比较复杂的TABHOST的处理~有需要的Q我。 转了下面的文章: ================ 本文演示如何在Android中实现程序前后台切换效果。    在介绍程序实现之前,我们先看下Android中Activities和Task的基础知识。   我们都知道,一个Activity...

VSCode——自定义VSCode背景图片

本文转载自https://blog.csdn.net/yukinoai/article/details/845649491.以管理员身份运行VS Code,安装background插件2.打开setting,在搜索框中输入background,选择扩展中的plugin background,选择在setting.json中编辑3.在用户设置中输入以下代码,...

python_14(js)

第1章 图片方法 1.1 设置背景图:1.2 背景图问题:1.3 background-repeat; noa-repe 1.4 background-attachment: fixed1.5 background-position 1.6 background-position-x 1.7 截取局部1.7.1 透明色第2章 定位 2.1 定义形式2.2...