AndroidTagView云标签
这是一个Android的云标签示例,这个View支持标签,点击,长按弹出删除对话框,拖拽排序,当前行超出屏幕宽度自动挤压到第二行……
截图
Usage使用
步骤 1
向build.gradle 添加依赖:
dependencies {
compile 'co.lujun:androidtagview:1.1.1'
}
步骤 2
在你的布局文件中使用AndroidTagView,你可以为AndroidTaghView自定义属性。
<co.lujun.androidtagview.TagContainerLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="10dp"
app:container_enable_drag="false"
app:horizontal_interval="10dp"
app:vertical_interval="10dp"
app:tag_clickable="true"
app:tag_theme="pure_teal" />
步骤 3
在你的代码中使用AndroidTagView:
TagContainerLayout mTagContainerLayout = (TagContainerLayout) findViewById(R.id.tagcontainerLayout);
mTagContainerLayout.setTags(List<String> tags);
现在,你已经成功的创建了TagView了。下文会展示更多有用的自定义属性:
属性
name | format | description |
---|---|---|
vertical_interval | dimension | 垂直间隔,默认是5dp |
horizontal_interval | dimension | 水平间隔,默认是5dp |
container_border_width | dimension | TagContainerLayout的边框粗细,默认是0.5dp |
container_border_radius | dimension | TagContainerLayout的边框圆角,默认是10.0dp |
container_border_color | color | TagContainerLayout 的边框颜色(默认是#22FF0000) |
container_background_color | color | TagContainerLayout的背景颜色(默认是#11FF0000) |
container_enable_drag | boolean | TagView是否可拖拽(默认是false) |
container_drag_sensitivity | float | TagView拖拽灵敏度(默认是1.0f, normal) |
container_gravity | enum | TagContainerLayout gravity的Gravity属性 |
container_max_lines | integer | TagContainerLayout的最大行数(默认是0, 自动增长) |
tag_border_width | dimension | TagView的边框狂赌(默认是0.5dp) |
tag_corner_radius | dimension | TagView的边框圆角(默认是15.0dp) |
tag_horizontal_padding | dimension | TagView的水平padding属性值, 包括left和right的padding属性值(左右的padding值是相同的, 默认是10dp) |
tag_vertical_padding | dimension | TagView的垂直padding属性值, 包括top和bottom的padding属性值(上下的padding值是相同的, 默认是8dp) |
tag_text_size | dimension | TagView的文字大小(默认是14sp) |
tag_bd_distance | dimension | 基线和 descent之间的距离(默认是2.75dp) |
tag_text_color | color | TagView的文字颜色(默认是 #FF666666) |
tag_border_color | color | TagView边框颜色(默认是 #88F44336) |
tag_background_color | color | TagView背景颜色(默认是#33F44336) |
tag_max_length | integer | TagView可以占到的最大长度(默认的最大长度是23) |
tag_clickable | boolean | TagView是否可点击(默认是不可点击unclickable) |
tag_theme | enum | TagView的 主题 |
tag_text_direction | enum | TagView的字体方向 |
tag_ripple_color | color | 涟漪效应色彩 (默认是#EEEEEE) |
tag_ripple_alpha | integer | 涟漪效应的Alpha值 (Alpha的范围在0 ~ 255之间, 默认是128) |
tag_ripple_duration | integer | 涟漪效应的持续事件(以毫秒为单位, 默认是1000ms) |
tag_enable_cross | boolean | 启用云标签的X图标(点击删除)(默认是false) |
tag_cross_width | dimension | 叉叉的宽度(叉叉的点击区域,默认等于tagview的高度) |
tag_cross_color | color | 叉叉的颜色(默认是Color.BLACK) |
tag_cross_line_width | dimension | 叉线的宽度(默认1dp) |
tag_cross_area_padding | dimension | 叉叉的padding值(默认是10dp) |
tag_support_letters_rlt | boolean | 是否支持“字母显示与RTL(如:Android -> diordna)风格(默认为false) |
You can set these attributes in layout file, or use setters(each attribute has get and set method) to set them.
你可以在你的布局文件中设置这些属性,或者使用set方法来为TagView设置属性。
主题
theme | code | value | description |
---|---|---|---|
none | ColorFactory.NONE | -1 | 自定义主题时,必须优先设置该参数 |
random | ColorFactory.RANDOM | 0 | 为每一个标签设置一个随机的颜色 |
pure_cyan | ColorFactory.PURE_CYAN | 1 | 所有的TagView使用纯青色 |
pure_teal | ColorFactory.PURE_TEAL | 2 | 所有的TagView使用纯绿色 |
标签文本方向
direction | code | value | description |
---|---|---|---|
ltr | View.TEXT_DIRECTION_LTR | 3 | 由左到右(默认) |
rtl | View.TEXT_DIRECTION_RTL | 4 | 由右到左 |
注:是标签内文本方向
标签文本水平对齐
gravity | code | value | description |
---|---|---|---|
left | Gravity.LEFT | 3 | 居左(默认) |
center | Gravity.CENTER | 17 | 居中 |
right | Gravity.RIGHT | 5 | 居右 |
方法
- 为TagView设置监听事件,有单击事件,长按事件,点击叉叉事件
mTagContainerLayout.setOnTagClickListener(new TagView.OnTagClickListener() {
@Override
public void onTagClick(int position, String text) {
// ...
}
@Override
public void onTagLongClick(final int position, String text) {
// ...
}
@Override
public void onTagCrossClick(int position) {
// ...
}
});
- 设置标签的最大长度
mTagContainerLayout.setTagMaxLength(int max);
- Use
getTagText(int position)
to get TagView text at the specified location.(通过position获取某个标签的文字)
String text = mTagContainerLayout.getTagText(int position);
- 获取所有标签的文字
List<String> list = mTagContainerLayout.getTags();
- 如果你把
container_enable_drag
属性设置为true
(即设置可拖拽模式), 当你拖拽一个TagView ,你可以使用getTagViewState()
获取当前TagView的状态位。 - 这里有三个状态位:
ViewDragHelper.STATE_IDLE
(空闲模式)ViewDragHelper.STATE_DRAGGING
(正在拖拽模式)ViewDragHelper.STATE_SETTLING
(正在设置模式)
int state = mTagContainerLayout.getTagViewState();
- 设置主题
- 如果你想要自定义TagView的主题,首先要设置主题属性值为
ColorFactory.NONE
,然后再设置其他的主题属性
// Set library provides theme
mTagContainerLayout.setTheme(ColorFactory.PURE_CYAN);
// Set customize theme
mTagContainerLayout.setTheme(ColorFactory.NONE);
mTagContainerLayout.setTagBackgroundColor(Color.TRANSPARENT);
- 设置文本方向
- 总共致支持两种方向
View.TEXT_DIRECTION_LTR
和View.TEXT_DIRECTION_RTL
。
mTagContainerLayout.setTagTextDirection(View.TEXT_DIRECTION_RTL);
- 使用
setTagTypeface(Typeface typeface)
设置标签的字体样式
Typeface typeface = Typeface.createFromAsset(getAssets(), "iran_sans.ttf");
mTagContainerLayout.setTagTypeface(typeface);
设置完标签的属性之后,可以设置tags或者是添加一个tag
- 设置标签,参数为 List 或 String[]:setTags()
mTagContainerLayout.setTags(List<String> tags);
- 追加一个标签
mTagContainerLayout.addTag(String text);
- 定点(插入位置为定点之前)插入一个标签
mTagContainerLayout.addTag(String text, int position);
- 删除指定位置的标签
mTagContainerLayout.removeTag(int position);
- 删除所有标签
mTagContainerLayout.removeAllTags();
- 为每一个TagView设置颜色
List<int[]> colors = new ArrayList<int[]>();
//int[] color = {TagBackgroundColor, TabBorderColor, TagTextColor}
int[] color1 = {Color.RED, Color.BLACK, Color.WHITE};
int[] color2 = {Color.BLUE, Color.BLACK, Color.WHITE};
colors.add(color1);
colors.add(color2);
mTagcontainerLayout.setTags(tags, colors);
版本迭代日志
1.1.1(2017-4-16)
1.1.0(2017-3-5)
- Fixed issue #45
- Support ‘letters show with RTL(eg: Android -> diordnA)’ style
1.0.6(2017-2-14)
- Fix bugs
1.0.5(2016-11-9)
- Add cross view for TagView
1.0.4(2016-10-30)
- Support ripple effect(Call requires API level 11), like Android CustomButton
- Fix bugs
1.0.3(2016-4-3)
- Add
getTags()
method to get the list for all tags - Fixed bugs in ListView/RecyclerView
1.0.2(2016-1-18)
- Support gravity for
TagContainerLayout
- Support set typeface
1.0.1(2016-1-14)
- Support text direction
- Add
removeAllTags()
method for remove all TagViews - Fixed issue #1
- Fixed other bugs
1.0.0(2016-1-6)
- First release
Sample App
About
If you have any questions, contact me: lujun.byte#gmail.com.
License
Copyright 2015 lujun
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.