Skip to content

Commit

Permalink
补充使用方法示例
Browse files Browse the repository at this point in the history
  • Loading branch information
alidili committed Jan 24, 2018
1 parent 940a7de commit 6b2080a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 24 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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" />
</RelativeLayout>
```
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/com/yang/flowlayout/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

</RelativeLayout>
4 changes: 2 additions & 2 deletions flowlayoutlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 13
versionName "1.3"
versionCode 14
versionName "1.4"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Line> lines = new ArrayList<Line>();
Expand All @@ -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) {
Expand Down Expand Up @@ -248,52 +248,104 @@ public void setFlowLayout(List<String> 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());
Expand Down

0 comments on commit 6b2080a

Please sign in to comment.