├── .gitignore ├── README.md ├── build.gradle ├── demo ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── bingoogolapple │ │ └── flowlayout │ │ └── demo │ │ └── MainActivity.java │ └── res │ ├── drawable │ ├── selector_tag.xml │ └── shape_stroke_line.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-v21 │ └── styles.xml │ └── values │ ├── colors.xml │ ├── strings.xml │ ├── styles.xml │ └── styles_base.xml ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── library ├── build.gradle ├── gradle.properties └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── bingoogolapple │ │ └── flowlayout │ │ └── BGAFlowLayout.java │ └── res │ └── values │ └── attrs.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Eclipse project files 30 | .classpath 31 | .project 32 | .settings/ 33 | 34 | # Intellij project files 35 | *.iml 36 | *.ipr 37 | *.iws 38 | .idea/ 39 | 40 | # Mac system files 41 | .DS_Store 42 | 43 | *.keystore -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | :running:BGAFlowLayout-Android:running: 2 | ============ 3 | 4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/cn.bingoogolapple/bga-flowlayout/badge.svg)](https://maven-badges.herokuapp.com/maven-central/cn.bingoogolapple/bga-flowlayout) 5 | 6 | Android流式布局,可配置是否将每一行的空白区域平均分配给子控件。 7 | 8 | 最开始是参考[鸿洋_的这篇文章](http://blog.csdn.net/lmj623565791/article/details/38352503)的思路实现的,后来根据产品经理出的需求,增加了将每一行的空白区域平均分配给子控件。 9 | 10 | demo中分别演示了在xml使用方式和在java代码中动态添加 11 | 12 | ### 效果图 13 | 14 | ![Image of 平均分配剩余空间](http://7xk9dj.com1.z0.glb.clouddn.com/flowlayout/screenshots/bga-flowlayout-demo1.gif) 15 | ![Image of 不平均分配剩余空间](http://7xk9dj.com1.z0.glb.clouddn.com/flowlayout/screenshots/bga-flowlayout-demo2.gif) 16 | 17 | ### Gradle依赖 18 | 19 | ```groovy 20 | dependencies { 21 | compile 'cn.bingoogolapple:bga-flowlayout:latestVersion@aar' 22 | } 23 | ``` 24 | 25 | ### 自定义属性说明 26 | 27 | ```xml 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | ``` 37 | 38 | ## 作者联系方式 39 | 40 | | 个人主页 | 邮箱 | 41 | | ------------- | ------------ | 42 | | bingoogolapple.cn | bingoogolapple@gmail.com | 43 | 44 | | 个人微信号 | 微信群 | 公众号 | 45 | | ------------ | ------------ | ------------ | 46 | | 个人微信号 | 微信群 | 公众号 | 47 | 48 | | 个人 QQ 号 | QQ 群 | 49 | | ------------ | ------------ | 50 | | 个人 QQ 号 | QQ 群 | 51 | 52 | ## 打赏支持作者 53 | 54 | 如果您觉得 BGA 系列开源库或工具软件帮您节省了大量的开发时间,可以扫描下方的二维码打赏支持。您的支持将鼓励我继续创作,打赏后还可以加我微信免费开通一年 [上帝小助手浏览器扩展/插件开发平台](https://github.com/bingoogolapple/bga-god-assistant-config) 的会员服务 55 | 56 | | 微信 | QQ | 支付宝 | 57 | | ------------- | ------------- | ------------- | 58 | | 微信 | QQ | 支付宝 | 59 | 60 | ## 作者项目推荐 61 | 62 | * 欢迎您使用我开发的第一个独立开发软件产品 [上帝小助手浏览器扩展/插件开发平台](https://github.com/bingoogolapple/bga-god-assistant-config) 63 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.0.0-alpha2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion ANDROID_BUILD_SDK_VERSION as int 5 | buildToolsVersion ANDROID_BUILD_TOOLS_VERSION 6 | 7 | defaultConfig { 8 | minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION as int 9 | targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION as int 10 | versionCode VERSION_CODE as int 11 | versionName VERSION_NAME 12 | } 13 | } 14 | 15 | dependencies { 16 | compile 'com.android.support:appcompat-v7:23.1.1' 17 | compile 'cn.bingoogolapple:bga-flowlayout:1.0.0@aar' 18 | // compile project(':library') 19 | } 20 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /demo/src/main/java/cn/bingoogolapple/flowlayout/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.bingoogolapple.flowlayout.demo; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.text.TextUtils; 7 | import android.view.Gravity; 8 | import android.view.KeyEvent; 9 | import android.view.ViewGroup; 10 | import android.view.inputmethod.EditorInfo; 11 | import android.widget.EditText; 12 | import android.widget.TextView; 13 | 14 | import cn.bingoogolapple.flowlayout.BGAFlowLayout; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | private String[] mVals = new String[]{"bingo", "googol", "apple", "bingoogolapple", "helloworld"}; 18 | private BGAFlowLayout mFlowLayout; 19 | private EditText mTagEt; 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_main); 25 | mTagEt = (EditText) findViewById(R.id.et_main_tag); 26 | mFlowLayout = (BGAFlowLayout) findViewById(R.id.flowlayout); 27 | initData(); 28 | 29 | 30 | mTagEt.setOnEditorActionListener(new TextView.OnEditorActionListener() { 31 | @Override 32 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 33 | if (actionId == EditorInfo.IME_ACTION_GO) { 34 | String tag = mTagEt.getText().toString().trim(); 35 | if (!TextUtils.isEmpty(tag)) { 36 | mFlowLayout.addView(getLabel(tag), mFlowLayout.getChildCount() - 1, new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT)); 37 | } 38 | mTagEt.setText(""); 39 | } 40 | return true; 41 | } 42 | }); 43 | } 44 | 45 | public void initData() { 46 | for (int i = 0; i < mVals.length; i++) { 47 | mFlowLayout.addView(getLabel(mVals[i]), mFlowLayout.getChildCount() - 1, new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT)); 48 | } 49 | } 50 | 51 | private TextView getLabel(String text) { 52 | TextView label = new TextView(this); 53 | label.setTextColor(Color.WHITE); 54 | label.setBackgroundResource(R.drawable.selector_tag); 55 | label.setGravity(Gravity.CENTER); 56 | label.setSingleLine(true); 57 | label.setEllipsize(TextUtils.TruncateAt.END); 58 | int padding = BGAFlowLayout.dp2px(this, 5); 59 | label.setPadding(padding, padding, padding, padding); 60 | label.setText(text); 61 | return label; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable/selector_tag.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /demo/src/main/res/drawable/shape_stroke_line.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 15 | 16 | 19 | 20 | 23 | 24 | 27 | 28 | 31 | 32 | 36 | 37 | 40 | 41 | 44 | 45 | 48 | 49 | 52 | 53 | 56 | 57 | 60 | 61 | 64 | 65 | 68 | 69 | 70 | 71 | 77 | 78 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingoogolapple/BGAFlowLayout-Android/c371484b5f504d047c7461773806e25e7fe45b2f/demo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingoogolapple/BGAFlowLayout-Android/c371484b5f504d047c7461773806e25e7fe45b2f/demo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingoogolapple/BGAFlowLayout-Android/c371484b5f504d047c7461773806e25e7fe45b2f/demo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingoogolapple/BGAFlowLayout-Android/c371484b5f504d047c7461773806e25e7fe45b2f/demo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /demo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #f57257 4 | #f46444 5 | #f46444 6 | 7 | #cccc 8 | -------------------------------------------------------------------------------- /demo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | BGAFlowLayoutDemo 3 | -------------------------------------------------------------------------------- /demo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 12 | 13 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /demo/src/main/res/values/styles_base.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 13 | 14 | 18 | 19 | 23 | 24 | 29 | 30 | 35 | 36 | 41 | 42 | 47 | 48 | 51 | 52 | 55 | 56 | 59 | 60 | 63 | 64 | 67 | 68 | 71 | 72 | 75 | 76 | 79 | 80 | 83 | 84 | 87 | 88 | 91 | 92 | 95 | 96 | 99 | 100 | 103 | 104 | 107 | 108 | 111 | 112 | 116 | 117 | 121 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ANDROID_BUILD_MIN_SDK_VERSION=10 2 | ANDROID_BUILD_TARGET_SDK_VERSION=23 3 | ANDROID_BUILD_SDK_VERSION=23 4 | ANDROID_BUILD_TOOLS_VERSION=23.0.2 5 | 6 | VERSION_NAME=1.0.0 7 | VERSION_CODE=100 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bingoogolapple/BGAFlowLayout-Android/c371484b5f504d047c7461773806e25e7fe45b2f/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Dec 04 13:08:31 CST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion ANDROID_BUILD_SDK_VERSION as int 5 | buildToolsVersion ANDROID_BUILD_TOOLS_VERSION 6 | 7 | defaultConfig { 8 | minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION as int 9 | targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION as int 10 | } 11 | } 12 | 13 | // gradle uploadArchives 14 | apply from: 'https://raw.githubusercontent.com/bingoogolapple/PublishAar/master/central-publish.gradle' -------------------------------------------------------------------------------- /library/gradle.properties: -------------------------------------------------------------------------------- 1 | PUBLISH_AAR_ARTIFACT_ID=bga-flowlayout 2 | PUBLISH_AAR_DESCRIPTION=Android FlowLayout Library 3 | PUBLISH_AAR_GITHUB_REPOSITORIES_NAME=BGAFlowLayout-Android -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /library/src/main/java/cn/bingoogolapple/flowlayout/BGAFlowLayout.java: -------------------------------------------------------------------------------- 1 | package cn.bingoogolapple.flowlayout; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.util.AttributeSet; 6 | import android.util.TypedValue; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | /** 14 | * 作者:王浩 邮件:bingoogolapple@gmail.com 15 | * 创建时间:15/6/24 11:21 16 | * 描述:流式布局 17 | */ 18 | public class BGAFlowLayout extends ViewGroup { 19 | private List mRows = new ArrayList<>(); 20 | private int mHorizontalChildGap; 21 | private int mVerticalChildGap; 22 | private boolean mIsDistributionWhiteSpacing = true; 23 | 24 | public BGAFlowLayout(Context context) { 25 | this(context, null); 26 | } 27 | 28 | public BGAFlowLayout(Context context, AttributeSet attrs) { 29 | this(context, attrs, 0); 30 | } 31 | 32 | public BGAFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) { 33 | super(context, attrs, defStyleAttr); 34 | initDefaultAttrs(context); 35 | initCustomAttrs(context, attrs); 36 | } 37 | 38 | private void initDefaultAttrs(Context context) { 39 | mHorizontalChildGap = dp2px(context, 10); 40 | mVerticalChildGap = dp2px(context, 10); 41 | } 42 | 43 | private void initCustomAttrs(Context context, AttributeSet attrs) { 44 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BGAFlowLayout); 45 | final int N = typedArray.getIndexCount(); 46 | for (int i = 0; i < N; i++) { 47 | initCustomAttr(typedArray.getIndex(i), typedArray); 48 | } 49 | typedArray.recycle(); 50 | } 51 | 52 | private void initCustomAttr(int attr, TypedArray typedArray) { 53 | if (attr == R.styleable.BGAFlowLayout_fl_horizontalChildGap) { 54 | /** 55 | * getDimension和getDimensionPixelOffset的功能差不多,都是获取某个dimen的值,如果是dp或sp的单位,将其乘以density,如果是px,则不乘;两个函数的区别是一个返回float,一个返回int. getDimensionPixelSize则不管写的是dp还是sp还是px,都会乘以denstiy. 56 | */ 57 | mHorizontalChildGap = typedArray.getDimensionPixelOffset(attr, mHorizontalChildGap); 58 | } else if (attr == R.styleable.BGAFlowLayout_fl_verticalChildGap) { 59 | mVerticalChildGap = typedArray.getDimensionPixelOffset(attr, mVerticalChildGap); 60 | } else if (attr == R.styleable.BGAFlowLayout_fl_isDistributionWhiteSpacing) { 61 | mIsDistributionWhiteSpacing = typedArray.getBoolean(attr, mIsDistributionWhiteSpacing); 62 | } 63 | } 64 | 65 | @Override 66 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 67 | int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); 68 | int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); 69 | /** 70 | * 1.EXACTLY:100dp,match_parent 71 | * 2.AT_MOST:wrap_content 72 | * 3.UNSPCIFIED:子控件想要多大就多大,很少见(ScrollView) 73 | */ 74 | int modeHeight = MeasureSpec.getMode(heightMeasureSpec); 75 | 76 | mRows.clear(); 77 | Row row = new Row(sizeWidth); 78 | 79 | View child; 80 | int childCount = getChildCount(); 81 | for (int i = 0; i < childCount; i++) { 82 | child = getChildAt(i); 83 | measureChild(child, widthMeasureSpec, heightMeasureSpec); 84 | if (!row.addChild(child)) { 85 | mRows.add(row); 86 | row = new Row(sizeWidth); 87 | row.addChild(child); 88 | } 89 | } 90 | 91 | // 添加最后一行 92 | if (!mRows.contains(row)) { 93 | mRows.add(row); 94 | } 95 | 96 | int height = 0; 97 | int rowCount = mRows.size(); 98 | for (int i = 0; i < rowCount; i++) { 99 | height += mRows.get(i).mHeight; 100 | if (i != rowCount - 1) { 101 | height += mVerticalChildGap; 102 | } 103 | } 104 | setMeasuredDimension(sizeWidth, modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom()); 105 | } 106 | 107 | @Override 108 | protected void onLayout(boolean changed, int l, int t, int r, int b) { 109 | int rowCount = mRows.size(); 110 | int top = getPaddingTop(); 111 | Row row; 112 | for (int i = 0; i < rowCount; i++) { 113 | row = mRows.get(i); 114 | if (mIsDistributionWhiteSpacing && i != rowCount - 1) { 115 | row.layout(true, top); 116 | } else { 117 | row.layout(false, top); 118 | } 119 | top += row.mHeight + mVerticalChildGap; 120 | } 121 | } 122 | 123 | private class Row { 124 | private List mViews = new ArrayList<>(); 125 | private int mWidth; 126 | private int mNewWidth; 127 | private int mHeight; 128 | private int mMaxWidth; 129 | 130 | public Row(int sizeWidth) { 131 | mMaxWidth = sizeWidth - getPaddingLeft() - getPaddingRight(); 132 | } 133 | 134 | public boolean addChild(View child) { 135 | if (isOutOfMaxWidth(child.getMeasuredWidth())) { 136 | return false; 137 | } else { 138 | mViews.add(child); 139 | mWidth = mNewWidth; 140 | 141 | int childHeight = child.getMeasuredHeight(); 142 | mHeight = mHeight < childHeight ? childHeight : mHeight; 143 | return true; 144 | } 145 | } 146 | 147 | private boolean isOutOfMaxWidth(int childWidth) { 148 | if (mViews.size() == 0) { 149 | mNewWidth = mWidth + childWidth; 150 | } else { 151 | mNewWidth = mWidth + mHorizontalChildGap + childWidth; 152 | } 153 | return mNewWidth > mMaxWidth; 154 | } 155 | 156 | /** 157 | * @param isNeedSplit 是否需要平均分配空白区域给每一个子孩子 158 | * @param top 159 | */ 160 | public void layout(boolean isNeedSplit, int top) { 161 | if (mViews.size() == 0) { 162 | return; 163 | } 164 | 165 | int left = getPaddingLeft(); 166 | int count = mViews.size(); 167 | int splitWidth = (mMaxWidth - mWidth) / count; 168 | View view; 169 | for (int i = 0; i < count; i++) { 170 | view = mViews.get(i); 171 | int childWidth = view.getMeasuredWidth(); 172 | int childHeight = view.getMeasuredHeight(); 173 | if (isNeedSplit) { 174 | childWidth = childWidth + splitWidth; 175 | view.getLayoutParams().width = childWidth; 176 | if (splitWidth > 0) { 177 | /** 178 | * 1.EXACTLY:100dp,match_parent 179 | * 2.AT_MOST:wrap_content 180 | * 3.UNSPCIFIED:子控件想要多大就多大,很少见(ScrollView) 181 | */ 182 | int widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY); 183 | int heightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY); 184 | view.measure(widthMeasureSpec, heightMeasureSpec); 185 | } 186 | } 187 | 188 | int topOffset = (int) ((mHeight - childHeight) / 2.0 + 0.5); 189 | view.layout(left, top + topOffset, left + childWidth, top + topOffset + childHeight); 190 | 191 | left += childWidth + mHorizontalChildGap; 192 | } 193 | } 194 | } 195 | 196 | public static int dp2px(Context context, float dpValue) { 197 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics()); 198 | } 199 | } -------------------------------------------------------------------------------- /library/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':library', ':demo' 2 | --------------------------------------------------------------------------------