Android利用FlexboxLayout輕松實(shí)現(xiàn)流動(dòng)布局
前言
相信大家應(yīng)該都有所體會(huì),在以前我們要實(shí)現(xiàn)流動(dòng)性布局,比較繁瑣,Google開源了一個(gè)項(xiàng)目叫FlexboxLayout,相信大家都不陌生。下面我們來(lái)學(xué)習(xí)一下FlexboxLayout基礎(chǔ)知識(shí),并通過(guò)一個(gè)案例來(lái)加深理解。如果你對(duì)FlexboxLayout很熟悉,請(qǐng)忽略本文。
一、什么是 Flexbox
簡(jiǎn)單來(lái)說(shuō) Flexbox 是屬于web前端領(lǐng)域CSS的一種布局方案,是2009年W3C提出了一種新的布局方案,可以響應(yīng)式地實(shí)現(xiàn)各種頁(yè)面布局,并且 React Native 也是使用的 Flex 布局。
我們可以簡(jiǎn)單的理解為 Flexbox 是CSS領(lǐng)域類似 Linearlayout 的一種布局,但比 Linearlayout 要強(qiáng)大的多。
二、 什么是 FlexboxLayout?
我們?cè)?Android 開發(fā)中使用 Linearlayout + RelativeLayout 基本可以實(shí)現(xiàn)大部分復(fù)雜的布局,但是Google就想了,有沒(méi)有類似 Flexbox 的一個(gè)布局呢?這使用起來(lái)一個(gè)布局就可以搞定各種復(fù)雜的情況了,于是 FlexboxLayout 就應(yīng)運(yùn)而生了。
所以 FlexboxLayout 是針對(duì) Android 平臺(tái)的,實(shí)現(xiàn)類似 Flexbox 布局方案的一個(gè)開源項(xiàng)目
我們先看看官方Demo的效果圖
開源地址:https://github.com/google/flexbox-layout
本地下載:點(diǎn)擊這里
三、使用方式
使用方式很簡(jiǎn)單,只需要添加以下依賴:
compile 'com.google.android:flexbox:0.2.2'
在xml布局中我們可以這樣使用
<com.google.android.flexbox.FlexboxLayout android:id="@+id/flexbox_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:flexWrap="wrap"> <TextView android:id="@+id/tv1" android:layout_width="120dp" android:layout_height="80dp" app:layout_flexBasisPercent="50%" /> <TextView android:id="@+id/tv2" android:layout_width="80dp" android:layout_height="80dp" app:layout_alignSelf="center"/> <TextView android:id="@+id/tv3" android:layout_width="160dp" android:layout_height="80dp" app:layout_alignSelf="flex_end"/> </com.google.android.flexbox.FlexboxLayout>
代碼中可以這樣使用
FlexboxLayout flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout); flexboxLayout.setFlexDirection(FlexboxLayout.FLEX_DIRECTION_COLUMN); View view = flexboxLayout.getChildAt(0); FlexboxLayout.LayoutParams lp = (FlexboxLayout.LayoutParams) view.getLayoutParams(); lp.order = -1; lp.flexGrow = 2; view.setLayoutParams(lp);
我們來(lái)看看要模仿的布局
下面我們來(lái)實(shí)現(xiàn)它,先來(lái)看最終實(shí)現(xiàn)的效果:
實(shí)現(xiàn)方法如下:
1. 新建activity_flow.xml布局
<?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" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.flexbox.FlexboxLayout android:id="@+id/flexbox_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:flexWrap="wrap" /> </RelativeLayout>
布局很簡(jiǎn)單,只有一個(gè)FlexboxLayout 因?yàn)槲覀冃枰獎(jiǎng)討B(tài)創(chuàng)建它的item,所以就在這里固定寫TextView了
2. 新建ActivityFlow Activity,填充數(shù)據(jù)源
String[] tags = {"婚姻育兒", "散文", "設(shè)計(jì)", "上班這點(diǎn)事兒", "影視天堂", "大學(xué)生活", "美人說(shuō)", "運(yùn)動(dòng)和健身", "工具癖", "生活家", "程序員", "想法", "短篇小說(shuō)", "美食", "教育", "心理", "奇思妙想", "美食", "攝影"}; flexboxLayout = (FlexboxLayout) findViewById(R.id.flexbox_layout); for (int i = 0; i < tags.length; i++) { Book model = new Book(); model.setId(i); model.setName(tags[i]); flexboxLayout.addView(createNewFlexItemTextView(model)); }
其中Book為一個(gè)實(shí)體,這個(gè)不是關(guān)鍵,關(guān)鍵的是createNewFlexItemTextView方法
我們要?jiǎng)討B(tài)加載FlexboxLayout其FlexItem 并且讓FlexboxLayout中的item支持點(diǎn)擊事件,因?yàn)槲覀冃枰烙脩酎c(diǎn)擊了哪個(gè)專題跳轉(zhuǎn)。
我們來(lái)看一下createNewFlexItemTextView方法
/** * 動(dòng)態(tài)創(chuàng)建TextView * @param book * @return */ private TextView createNewFlexItemTextView(final Book book) { TextView textView = new TextView(this); textView.setGravity(Gravity.CENTER); textView.setText(book.getName()); textView.setTextSize(12); textView.setTextColor(getResources().getColor(R.color.colorAccent)); textView.setBackgroundResource(R.drawable.tag_states); textView.setTag(book.getId()); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e(TAG, book.getName()); } }); int padding = Util.dpToPixel(this, 4); int paddingLeftAndRight = Util.dpToPixel(this, 8); ViewCompat.setPaddingRelative(textView, paddingLeftAndRight, padding, paddingLeftAndRight, padding); FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int margin = Util.dpToPixel(this, 6); int marginTop = Util.dpToPixel(this, 16); layoutParams.setMargins(margin, marginTop, margin, 0); textView.setLayoutParams(layoutParams); return textView; }
其他有關(guān)Book實(shí)體和Util類,也貼出來(lái)一下
Book實(shí)體
public class Book { private int id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Book() { } }
Util工具類
public class Util { public static int pixelToDp(Context context, int pixel) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); return pixel < 0 ? pixel : Math.round(pixel / displayMetrics.density); } public static int dpToPixel(Context context, int dp) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); return dp < 0 ? dp : Math.round(dp * displayMetrics.density); } }
這樣關(guān)于流動(dòng)布局[FlexboxLayout],我們就實(shí)現(xiàn)完成了,是不是很簡(jiǎn)單。
總結(jié)
以上就是關(guān)于Android輕松搞定流動(dòng)布局(FlexboxLayout)的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android startActivityForResult實(shí)例詳解
- Android在Fragment中實(shí)現(xiàn)監(jiān)聽觸摸事件
- Android 使用FragmentTabhost代替Tabhost
- Android數(shù)據(jù)持久化之File機(jī)制分析
- Android數(shù)據(jù)持久化之Preferences機(jī)制詳解
- Android 如何本地加載pdf文件
- android手機(jī)端與PC端使用adb forword通信
- Android 中SwipeRefreshLayout與ViewPager滑動(dòng)事件沖突解決方法
- Android 7.0行為變更 FileUriExposedException解決方法
相關(guān)文章
Android ListView 單條刷新方法實(shí)踐及原理解析
這篇文章主要介紹了Android ListView 單條刷新方法實(shí)踐及原理解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android實(shí)現(xiàn)選擇相冊(cè)圖片并顯示功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)選擇相冊(cè)圖片并顯示功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android中WebView實(shí)現(xiàn)點(diǎn)擊超鏈接啟動(dòng)QQ的方法
這篇文章主要給大家介紹了在Android中WebView如何實(shí)現(xiàn)點(diǎn)擊超鏈接啟動(dòng)QQ的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的學(xué)習(xí)或者工作具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04Android自定義View實(shí)現(xiàn)分段選擇按鈕的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)分段選擇按鈕的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Android中Fragment的分屏顯示處理橫豎屏顯示的實(shí)現(xiàn)方法
今天小編就為大家分享一篇關(guān)于Android中Fragment的分屏顯示處理橫豎屏顯示的實(shí)現(xiàn)方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Android自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)價(jià)格區(qū)間選擇控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11Android開發(fā)中的9個(gè)常見錯(cuò)誤和解決方法
這篇文章主要介紹了Android開發(fā)中的9個(gè)常見錯(cuò)誤和解決方法,這是Android開發(fā)中最常見的9個(gè)錯(cuò)誤,經(jīng)過(guò)各種各樣的整理,以及和熱心網(wǎng)友討論總結(jié)而來(lái),需要的朋友可以參考下2015-01-01