1. 程式人生 > >Android開發之玩轉FlexboxLayout佈局

Android開發之玩轉FlexboxLayout佈局

在這之前,我曾認真的研究過鴻洋大神的Android 自定義ViewGroup 實戰篇 -> 實現FlowLayout,按照大神的思路寫出了一個流式佈局,所有的東西都是難者不會會者不難,當自己能自定義流式佈局的時候就會覺得這東西原來很簡單了。如果各位小夥伴也看過那篇文章的話,應該知道自定義流式佈局還是非常麻煩的,不過Google今年開源了新的容器,就是這個FlexboxLayout,如果你玩過前端開發或者玩過RN,就會覺得這個FlexboxLayout真是簡單,OK,那我們今天就來看看這個FlexboxLayout的使用吧!先來看看顯示效果:

OK,我們來看看這個東東要怎麼實現吧!

1.引入專案

使用之前當然是先引入了,Google在GitHub上開源了這個控制元件,地址如下:

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  表示子元素的排列方向,元素的排列方向為主軸的方向,該屬性有四種取值,不同取值對應不同的主副軸,看下面一張圖:

預設為row,所以如果我給flexWrap取wrap_reverse屬性,則效果如下:

副軸取反,由於flexDirection預設為row,即副軸為豎直方向,副軸取反,即豎直方向倒著顯示。同理,如果我給flexDirection屬性設定為column,對應主軸方向為豎直向下,這個時候控制元件就會按如下方式來顯示:

其它值我就不一一測試了。

justifyContent 表示控制元件沿主軸對齊方向,有五種取值,預設情況下大家看到控制元件是左對齊(flex_start),另外還有主軸居中對齊(center):

主軸居右對齊(flex_end):

兩端對齊,子元素之間的間隔相等,但是兩端的子元素分別和左右兩邊的間距為0(space_between):

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

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">
		....
		....

效果:

大家看到系統會自動放大子元素的高度以使之填滿父容器。

與副軸起點對齊(flex_start):

程式碼:

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

與副軸終點對齊(flex_end):

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

還有兩個值,分別是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>

顯示效果:

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的三分之一。

以上。

相關推薦

Android開發FlexboxLayout佈局

在這之前,我曾認真的研究過鴻洋大神的Android 自定義ViewGroup 實戰篇 -> 實現FlowLayout,按照大神的思路寫出了一個流式佈局,所有的東西都是難者不會會者不難,當自己能自定義流式佈局的時候就會覺得這東西原來很簡單了。如果各位小夥伴也看過那篇文章的

Android開發FlexboxLayout佈局(可用於普通控制元件實現流式佈局,也可結合RecycleView實現流式佈局

在這之前,我曾認真的研究過鴻洋大神的Android 自定義ViewGroup 實戰篇 -> 實現FlowLayout,按照大神的思路寫出了一個流式佈局,所有的東西都是難者不會會者不難,當自己能自定義流式佈局的時候就會覺得這東西原來很簡單了。如果各位小夥伴也看過那篇

React戰記Flex佈局(上篇--容器屬性)

零、前言 最近一直在總結Android,前端這塊感覺忘得也差不多了 Flex佈局以前也聽過,但沒有詳細學習過,趁機會用React玩轉一下, 遇到一個新的知識怎麼學,一大堆的引數讓人發懵,我最喜歡的一句話:能應對變化的只有變化本身 用自己的能力讓學習的物件非靜態,就像與你交流的是一個活的人而非人偶 本文

android開發四種基本佈局

1.LinerLayout 又稱線性佈局 特有屬性: layout_weight按照比例指定控制元件大小 2.RelativeLayout 又稱相對佈局 常用屬性: <ImageView android:id="@+id/imageViews"

Android開發流式標籤佈局

1、流式佈局的特點以及應用場景    特點:當上面一行的空間不夠容納新的TextView時候,才開闢下一行的空間。主要用於關鍵詞搜尋或者熱門標籤等場景。 2、自定義ViewGroup,重點重寫下面兩個方法     (1) onMeasure:測量子view的寬高,設

Android開發增量更新

avt exp chm 這一 font ams extern city ron 一、使用場景 apk升級,節省服務器和用戶的流量 二、原理 自從 Android 4.1 開始, Google Play 引入了應用程序的增量更新功能,App使用該升級方式,可節省約2/3

android開發fragment與activity之間相互跳

   Fragment的產生與介紹 Android執行在各種各樣的裝置中,有小螢幕的手機,超大屏的平板甚至電視。針對螢幕尺寸的差距,很多情況下,都是先針對手機開發一套App,然後拷貝一份,修改佈局以適應平板神馬超級大屏的。難道無法做到一個App可以同時適應手機和平板麼

Android開發Activity的建立跳及傳值

在Android系統的江湖中有四大元件:活動(Activity), 服務(Service), 廣播接收器(Broadcast Reciver)和內容提供者(Content Provider)。今天所介紹的就是Android開發中的四大元件之一:Activity,其他那三大元件

Android開發基本控制元件和詳解四種佈局方式

Android中的控制元件的使用方式和iOS中控制元件的使用方式基本相同,都是事件驅動。給控制元件新增事件也有介面回撥和委託代理的方式。今天這篇部落格就總結一下Android中常用的基本控制元件以及佈局方式。說到佈局方式Android和iOS還是區別挺大的,在iOS中有F

android開發-相對佈局的屬性

// 相對於給定ID控制元件 android:layout_above 將該控制元件的底部置於給定ID的控制元件之上; android:layout_below 將該控制元件的底部置於給定ID的控制元件之下; android:layout_toLeftOf    將該控制元

Android應用開發PDF圖片功能實現

一、概述 轉眼畢業小兩年了,回想這兩年寫的程式碼,不由得想起了上學那會兒某位網際網路大牛說,搞IT這行如果你沒有寫夠十萬行程式碼,那你就還沒有入門,俺這個時候真的有點汗顏啊,此處省略十萬字。小編最近開始整理這兩年寫的程式碼,不由得首先想起了今天要寫的這個功能—

Android開發RecyclerView實現流式佈局

RecyclerView是什麼? RecycleView的出現, 替代了ListView, 沒了OnitemClickListener,; LayoutManager負責計算佈局; Adapter 負責適配,還增加了ViewHolder;RecycleView

Android開發計算器(一)介面設計activity_main佈局檔案

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schem

[] Android開發如何保證Service不被殺掉(broadcast+system/app)

轉發:原文連結http://blog.csdn.net/mad1989/article/details/22492519 序言 最近專案要實現這樣一個效果:執行後,要有一個service始終保持在後臺執行,不管使用者作出什麼操作,都要保證service不被kill,這可真是一個難題。參考了現今各種定製版的系

Android開發線性佈局(LinearLayout)

老的Api(2.2或者2.3的)有五種佈局: LinearLayout線性佈局,Relative Layout相對佈局,AbsoluteLayout絕對位置佈局,FrameLayout幀佈局,TableLayout表格佈局 而在最新的Api中只有兩種佈局:LinearLa

Android開發實現蘋果瀏覽器網頁跳動畫

前言:上一章《Android開發之安卓屬性動畫大總結》大致介紹了安卓的屬性動畫以及使用時的注意點,今天我們就來一個小綜合案列,實現類似於蘋果瀏覽器的跳轉頁面! ----------------------分割線------------------------ 來看下效果圖:

Android開發UI佈局優化全面總結

Android開發最常見的問題之一是螢幕碎片化太嚴重,所以我們在寫佈局的時候儘量不能適應硬編碼去佈局。 佈局優化在開發過程中起到至關重要的作用。 1.合用weightSum屬性和layout_weig

Android開發Intent跳到系統應用中的撥號介面、聯絡人介面、簡訊介面

現在開發中的功能需要直接跳轉到撥號、聯絡人、簡訊介面等等,查找了很多資料,自己整理了一下。          首先,我們先看撥號介面,程式碼如下: Intent intent =new Intent();                 intent.setAction("an

Android開發--從app中跳到淘寶店鋪

首先、一個工具類   方法,檢測該包名下的應用是否存在 public static boolean checkPackage(Context context ,String packageName) { if (packageName == null || "".

AndroidMPAndroidChart讓(折線圖、柱形圖、餅狀圖、雜湊圖、雷達圖)優雅的舞動

package com.example.chenyu.mpandroidcharttest; import android.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.suppor