diff --git a/README.md b/README.md index 554f385..15bcad0 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,25 @@ public class MainActivity extends AppCompatActivity { list.add("关键词四"); list.add("关键词五"); - // 设置TextView水平margin + // 设置文字大小 + flKeyword.setTextSize(15); + // 设置文字颜色 + flKeyword.setTextColor(Color.BLACK); + // 设置文字背景 + flKeyword.setBackgroundResource(R.drawable.bg_frame); + + // 设置文字水平margin flKeyword.setHorizontalSpacing(15); - // 设置TextView垂直margin + // 设置文字垂直margin flKeyword.setVerticalSpacing(15); - // 设置TextView水平padding + + // 设置文字水平padding flKeyword.setTextPaddingH(15); - // 设置TextView垂直padding + // 设置文字垂直padding flKeyword.setTextPaddingH(8); // 设置UI与点击事件监听 + // 最后调用setFlowLayout方法 flKeyword.setFlowLayout(list, new FlowLayout.OnItemClickListener() { @Override public void onItemClick(String content) { @@ -81,8 +90,13 @@ public class MainActivity extends AppCompatActivity { android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" + app:backgroundResource="@drawable/bg_frame" + app:horizontalSpacing="15dp" app:itemColor="@color/colorAccent" - app:itemSize="15sp" /> + app:itemSize="15sp" + app:textPaddingH="15dp" + app:textPaddingV="8dp" + app:verticalSpacing="15dp" /> ``` diff --git a/app/build.gradle b/app/build.gradle index 117473a..0a1130b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.yang.flowlayout" minSdkVersion 15 targetSdkVersion 26 - versionCode 13 - versionName "1.3" + versionCode 14 + versionName "1.4" } buildTypes { release { diff --git a/app/src/main/java/com/yang/flowlayout/MainActivity.java b/app/src/main/java/com/yang/flowlayout/MainActivity.java index e13a6b2..d770910 100644 --- a/app/src/main/java/com/yang/flowlayout/MainActivity.java +++ b/app/src/main/java/com/yang/flowlayout/MainActivity.java @@ -1,5 +1,6 @@ package com.yang.flowlayout; +import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; @@ -26,16 +27,25 @@ protected void onCreate(Bundle savedInstanceState) { list.add("关键词四"); list.add("关键词五"); - // 设置TextView水平margin + // 设置文字大小 + flKeyword.setTextSize(15); + // 设置文字颜色 + flKeyword.setTextColor(Color.BLACK); + // 设置文字背景 + flKeyword.setBackgroundResource(R.drawable.bg_frame); + + // 设置文字水平margin flKeyword.setHorizontalSpacing(15); - // 设置TextView垂直margin + // 设置文字垂直margin flKeyword.setVerticalSpacing(15); - // 设置TextView水平padding + + // 设置文字水平padding flKeyword.setTextPaddingH(15); - // 设置TextView垂直padding + // 设置文字垂直padding flKeyword.setTextPaddingH(8); // 设置UI与点击事件监听 + // 最后调用setFlowLayout方法 flKeyword.setFlowLayout(list, new FlowLayout.OnItemClickListener() { @Override public void onItemClick(String content) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e1229d8..40fc154 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -11,7 +11,12 @@ android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" + app:backgroundResource="@drawable/bg_frame" + app:horizontalSpacing="15dp" app:itemColor="@color/colorAccent" - app:itemSize="15sp" /> + app:itemSize="15sp" + app:textPaddingH="15dp" + app:textPaddingV="8dp" + app:verticalSpacing="15dp" /> diff --git a/flowlayoutlibrary/build.gradle b/flowlayoutlibrary/build.gradle index e993981..575aeee 100644 --- a/flowlayoutlibrary/build.gradle +++ b/flowlayoutlibrary/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 26 - versionCode 13 - versionName "1.3" + versionCode 14 + versionName "1.4" } buildTypes { release { diff --git a/flowlayoutlibrary/src/main/java/com/yang/flowlayoutlibrary/FlowLayout.java b/flowlayoutlibrary/src/main/java/com/yang/flowlayoutlibrary/FlowLayout.java index a30a4a7..a031481 100644 --- a/flowlayoutlibrary/src/main/java/com/yang/flowlayoutlibrary/FlowLayout.java +++ b/flowlayoutlibrary/src/main/java/com/yang/flowlayoutlibrary/FlowLayout.java @@ -15,14 +15,14 @@ import java.util.List; /** - * 关键词流式布局 + * 文字流式布局 * Created by yangle on 2016/10/10. */ public class FlowLayout extends RelativeLayout { // 水平间距,单位为dp private int horizontalSpacing = dp2px(15); - // 竖直间距,单位为dp + // 垂直间距,单位为dp private int verticalSpacing = dp2px(15); // 行的集合 private List lines = new ArrayList(); @@ -34,11 +34,11 @@ public class FlowLayout extends RelativeLayout { private int textSize = sp2px(15); // 关键字颜色 private int textColor = Color.BLACK; - // 关键字背景框 + // 关键字背景 private int backgroundResource = R.drawable.bg_frame; // 关键字水平padding,单位为dp private int textPaddingH = dp2px(15); - // 关键字竖直padding,单位为dp + // 关键字垂直padding,单位为dp private int textPaddingV = dp2px(8); public FlowLayout(Context context) { @@ -248,52 +248,104 @@ public void setFlowLayout(List list, final OnItemClickListener onItemCli this.addView(tv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - tv.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - onItemClickListener.onItemClick(tv.getText().toString()); - } - }); + if (onItemClickListener != null) { + tv.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + onItemClickListener.onItemClick(tv.getText().toString()); + } + }); + } } } + /** + * 点击事件监听 + */ public interface OnItemClickListener { void onItemClick(String content); } + /** + * 设置文字水平间距 + * + * @param horizontalSpacing 间距/dp + */ public void setHorizontalSpacing(int horizontalSpacing) { this.horizontalSpacing = dp2px(horizontalSpacing); } + /** + * 设置文字垂直间距 + * + * @param verticalSpacing 间距/dp + */ public void setVerticalSpacing(int verticalSpacing) { this.verticalSpacing = dp2px(verticalSpacing); } + /** + * 设置文字大小 + * + * @param textSize 文字大小/sp + */ public void setTextSize(int textSize) { this.textSize = sp2px(textSize); } + /** + * 设置文字颜色 + * + * @param textColor 文字颜色 + */ public void setTextColor(int textColor) { this.textColor = textColor; } + /** + * 设置文字背景 + * + * @param backgroundResource 文字背景 + */ public void setBackgroundResource(int backgroundResource) { this.backgroundResource = backgroundResource; } + /** + * 设置文字水平padding + * + * @param textPaddingH padding/dp + */ public void setTextPaddingH(int textPaddingH) { this.textPaddingH = dp2px(textPaddingH); } + /** + * 设置文字垂直padding + * + * @param textPaddingV padding/dp + */ public void setTextPaddingV(int textPaddingV) { this.textPaddingV = dp2px(textPaddingV); } + /** + * dp转px + * + * @param dp dp值 + * @return px值 + */ private int dp2px(float dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); } + /** + * sp转px + * + * @param sp sp值 + * @return px值 + */ private int sp2px(float sp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());