效果
編寫佈局
<com.donkingliang.labels.LabelsView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/labels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelBackground="@drawable/label_bg" //標籤的背景
app:labelTextColor="@drawable/label_text_color" //標籤的字體顏色 可以是一個顏色值
app:labelTextSize="14sp" //標籤的字體大小
app:labelTextPaddingBottom="5dp" //標籤的上下左右邊距
app:labelTextPaddingLeft="10dp"
app:labelTextPaddingRight="10dp"
app:labelTextPaddingTop="5dp"
app:lineMargin="10dp" //行與行的距離
app:wordMargin="10dp" //標籤與標籤的距離
app:selectType="SINGLE" //標籤的選擇類型 有單選、多選、不可選三種類型
app:maxSelect="5" /> //標籤的最大選擇數量,只有多選的時候纔有用,0爲不限數量
這裏有兩個地方需要說明一下: 1)標籤的正常樣式和選中樣式是通過drawable來實現的。比如下面兩個drawable。
<!-- 標籤的背景 label_bg -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 標籤選中時的背景 -->
<item android:state_selected="true">
<shape>
<stroke android:width="2dp" android:color="#fb435b" />
<corners android:radius="8dp" />
<solid android:color="@android:color/white" />
</shape>
</item>
<!-- 標籤的正常背景 -->
<item>
<shape>
<stroke android:width="2dp" android:color="#656565" />
<corners android:radius="8dp" />
<solid android:color="@android:color/white" />
</shape>
</item>
</selector>
<!-- 標籤的文字顏色 label_text_color -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 標籤選中時的文字顏色 -->
<item android:color="#fb435b" android:state_selected="true" />
<!-- 標籤的正常文字顏色 -->
<item android:color="#2d2b2b" />
</selector>
TextView的textColor屬性除了可以設置一個顏色值以外,也可以通過資源來設置的,這一點很多同學都不知道。 2)標籤的選擇類型有三種:
NONE :標籤不可選中,也不響應選中事件監聽,這是默認值。
SINGLE:單選。
MULTI:多選,可以通過設置maxSelect限定選擇的最大數量,0爲不限數量。maxSelect只有在多選的時候纔有效。
3、設置標籤:
labelsView = (LabelsView) findViewById(labels);
ArrayList<String> label = new ArrayList<>();
label.add("Android");
label.add("IOS");
label.add("前端");
label.add("後臺");
label.add("微信開發");
label.add("遊戲開發");
label.add("Java");
label.add("JavaScript");
label.add("C++");
label.add("PHP");
label.add("Python");
label.add("Swift");
labelsView.setLabels(label); //直接設置一個字符串數組就可以了。
4、設置事件監聽:(如果需要的話)
//標籤的點擊監聽
labelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
@Override
public void onLabelClick(View label, String labelText, int position) {
//label是被點擊的標籤,labelText是標籤的文字,position是標籤的位置。
}
});
//標籤的選中監聽
labelsView.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
@Override
public void onLabelSelectChange(View label, String labelText, boolean isSelect, int position) {
//label是被點擊的標籤,labelText是標籤的文字,isSelect是是否選中,position是標籤的位置。
}
});
5、常用方法
//設置選中標籤。
//positions是個可變類型,表示被選中的標籤的位置。
//比喻labelsView.setSelects(1,2,5);選中第1,3,5個標籤。如果是單選的話,只有第一個參數有效。
public void setSelects(int... positions);
//獲取選中的標籤。返回的是一個Integer的數組,表示被選中的標籤的下標。如果沒有選中,數組的size等於0。
public ArrayList<Integer> getSelectLabels();
//取消所有選中的標籤。
public void clearAllSelect();
//設置標籤的選擇類型,有NONE、SINGLE和MULTI三種類型。
public void setSelectType(SelectType selectType);
//設置最大的選擇數量,只有selectType等於MULTI是有效。
public void setMaxSelect(int maxSelect);
//設置標籤背景
public void setLabelBackgroundResource(int resId);
//設置標籤的文字顏色
public void setLabelTextColor(int color);
public void setLabelTextColor(ColorStateList color);
//設置標籤的文字大小(單位是px)
public void setLabelTextSize(float size);
//設置標籤內邊距
public void setLabelTextPadding(int left, int top, int right, int bottom);
//設置行間隔
public void setLineMargin(int margin);
//設置標籤的間隔
public void setWordMargin(int margin);
所以的set方法都有對應的get方法,這裏就不說了。