介紹
可自定義的標簽視圖,支持添加標簽,點擊,長按彈出刪除對話框,以及拖拽排序。
類似控件
android-flowlayout
TagCloudView
使用
第一步
在build.gradle文件中添加依賴:
dependencies {
compile 'co.lujun:androidtagview:1.0.3'
}
第二步
在布局中使用AndroidTagView,可以使用自定義屬性。
<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" />
第三步
在代碼中使用,并設置數據。
TagContainerLayout mTagContainerLayout = (TagContainerLayout) findViewById(R.id.tagcontainerLayout);
mTagContainerLayout.setTags(List<String> tags);
[圖片上傳失敗...(image-e318a2-1540455478741)]
屬性
以下屬性均可在布局中設置,也可以通過set或get方法使用。
名稱 | 類型 | 說明 |
---|---|---|
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 | 是否可以拖拽,默認false |
container_drag_sensitivity | float | 拖拽靈敏度,默認 1.0f, normal |
container_gravity | enum | The TagContainerLayout gravity |
tag_border_width | dimension | 子標簽 邊框粗細,默認 0.5dp |
tag_corner_radius | dimension | 子標簽 邊框圓角,默認 15.0dp) |
tag_horizontal_padding | dimension | 子標簽內部水平間距,包含左右兩邊,間距相同,默認20px |
tag_vertical_padding | dimension | 子標簽內部垂直間距,包含上下兩邊,間距相同,默認17px |
tag_text_size | dimension | 子標簽字體大小,默認14sp |
tag_bd_distance | dimension | 基線和 descent之間的距離,默認 5.5px |
tag_text_color | color | 子標簽 字體顏色,默認 #FF666666 |
tag_border_color | color | 子標簽 邊框顏色,默認 #88F44336 |
tag_background_color | color | 子標簽背景色,默認 #33F44336 |
tag_max_length | integer | 子標簽 文本長度最大值,默認 23 |
tag_clickable | boolean | 子標簽 是否可點擊,默認 不可點擊 |
tag_theme | enum | 子標簽 主題 |
tag_text_direction | enum | 子標簽 方向(文本方向) |
主題
theme | code | 取值 | 說明 |
---|---|---|---|
none | ColorFactory.NONE | -1 | 自定義主題時,必須優先設置該參數 |
random | ColorFactory.RANDOM | 0 | 所有標簽使用隨機顏色 |
pure_cyan | ColorFactory.PURE_CYAN | 1 | 所有標簽使用純青色 |
pure_teal | ColorFactory.PURE_TEAL | 2 | 所有標簽使用純藍綠色 |
方向
direction | code | 取值 | 說明 |
---|---|---|---|
ltr | View.TEXT_DIRECTION_LTR | 3 | 由左到右(默認) |
rtl | View.TEXT_DIRECTION_RTL | 4 | 由右到左 |
注:是標簽內文本方向
Gravity
gravity | code | 取值 | 說明 |
---|---|---|---|
left | Gravity.LEFT | 3 | 居左(默認) |
center | Gravity.CENTER | 17 | 居中 |
right | Gravity.RIGHT | 5 | 居右 |
注:是子標簽在父布局中的相對位置,父布局即引用的co.lujun.androidtagview.TagContainerLayout。
Methods
- 設置監聽事件:TagView.OnTagClickListener
mTagContainerLayout.setOnTagClickListener(new TagView.OnTagClickListener() {
@Override
public void onTagClick(int position, String text) {
// ...點擊事件
}
@Override
public void onTagLongClick(final int position, String text) {
// ...長按事件
}
});
- 設置標簽最大長度:setTagMaxLength(int max)
mTagContainerLayout.setTagMaxLength(int max);
- 獲取某個標簽的文字:getTagText(int position)
String text = mTagContainerLayout.getTagText(int position);
- 獲取所有標簽的文字:getTags()
List<String> list = mTagContainerLayout.getTags();
- 可拖拽模式(container_enable_drag="true")下獲取標簽狀態,:getTagViewState()
/**共3種狀態
1.ViewDragHelper.STATE_IDLE 空閑狀態
2.ViewDragHelper.STATE_DRAGGING 正在拖拽狀態
3.ViewDragHelper.STATE_SETTLING 正在設置狀態
**/
int state = mTagContainerLayout.getTagViewState();
- 設置主題,如果要自定義主題,切記先設置主題ColorFactory.NONE,然后再設置其他屬性setTheme(int theme)
// 設置自帶主題
mTagContainerLayout.setTheme(ColorFactory.PURE_CYAN);
.
// 設置自定義主題
mTagContainerLayout.setTheme(ColorFactory.NONE);
mTagContainerLayout.setTagBackgroundColor(Color.TRANSPARENT);
- 設置文本方向:setTagTextDirection(int direction)
/**共2種方向
1.View.TEXT_DIRECTION_LTR 由左到右
2.View.TEXT_DIRECTION_RTL 由右到左
**/
mTagContainerLayout.setTagTextDirection(View.TEXT_DIRECTION_RTL);
- 設置標簽的文本字體:setTagTypeface(Typeface typeface)
Typeface typeface = Typeface.createFromAsset(getAssets(), "iran_sans.ttf");
mTagContainerLayout.setTagTypeface(typeface);
- 設置標簽,參數為 List<String> 或 String[]:setTags()
mTagContainerLayout.setTags(List<String> tags);
- 添加單個標簽(末尾):addTag(String text)
mTagContainerLayout.addTag(String text);
- 在指定位置添加單個標簽(即當前位置的標簽之前),:addTag(String text, int position)
mTagContainerLayout.addTag(String text, int position);
- 刪除指定位置的標簽:removeTag(int position)
mTagContainerLayout.removeTag(int position);
- 刪除所有標簽:removeAllTags()
mTagContainerLayout.removeAllTags();