Android标签AndroidTagGroup(系列1)
AndroidTagGroup是github上开源的标签库,效果图如下:
AndroidTagGroup在github上的项目主页是:https://github.com/2dxgujun/AndroidTagGroup
AndroidTagGroup的使用:
(1)在布局文件写AndroidTagGroup,比如:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缺省(默认)" />
android:id="@+id/tag_group"
style="@style/TagGroup" />
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TagGroup.Beauty_Red" />
android:id="@+id/tag_group_beauty"
style="@style/TagGroup.Beauty_Red" />
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TagGroup.Beauty_Red.Inverse" />
android:id="@+id/tag_group_beauty_inverse"
style="@style/TagGroup.Beauty_Red.Inverse" />
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小号" />
android:id="@+id/tag_group_small"
style="@style/TagGroup.Small" />
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大号" />
android:id="@+id/tag_group_large"
style="@style/TagGroup.Large" />
(2)然后在Java代码中为其设置标签内容,比如随意设置一些标签字段:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import me.gujun.android.taggroup.TagGroup;
public class MainActivity extends Activity {
private TagGroup mDefaultTagGroup;
private TagGroup mSmallTagGroup;
private TagGroup mLargeTagGroup;
private TagGroup mBeautyTagGroup;
private TagGroup mBeautyInverseTagGroup;
private String[] tags = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 标签里面的字符串。
tags = new String[] { "Zhang", "Phil", "ZhangPhil", "CSDN Zhang Phil", "Android", "移动", "CSDN", "Android研发" };
mDefaultTagGroup = (TagGroup) findViewById(R.id.tag_group);
mDefaultTagGroup.setTags(tags);
mSmallTagGroup = (TagGroup) findViewById(R.id.tag_group_small);
mSmallTagGroup.setTags(tags);
mLargeTagGroup = (TagGroup) findViewById(R.id.tag_group_large);
mLargeTagGroup.setTags(tags);
mBeautyTagGroup = (TagGroup) findViewById(R.id.tag_group_beauty);
mBeautyTagGroup.setTags(tags);
mBeautyInverseTagGroup = (TagGroup) findViewById(R.id.tag_group_beauty_inverse);
mBeautyInverseTagGroup.setTags(tags);
// 为每一个标签增加点击事件。
mDefaultTagGroup.setOnTagClickListener(mTagClickListener);
mSmallTagGroup.setOnTagClickListener(mTagClickListener);
mLargeTagGroup.setOnTagClickListener(mTagClickListener);
mBeautyTagGroup.setOnTagClickListener(mTagClickListener);
mBeautyInverseTagGroup.setOnTagClickListener(mTagClickListener);
}
private TagGroup.OnTagClickListener mTagClickListener = new TagGroup.OnTagClickListener() {
@Override
public void onTagClick(String tag) {
Toast.makeText(getApplicationContext(), tag, Toast.LENGTH_SHORT).show();
}
};
}
可以通过为其增加点击事件以响应用户的点击事件。
如果需要再次二次定制AndroidTagGroup样式,则需要在AndroidTagGroup 的库项目中的res/values/styles.xml改写某些属性字段值:
比如,定义边线宽度的是:
1dp
又比如,标签的边线颜色和里面的文本颜色是:
#0097a7
#0097a7
届时可以根据自己需要再次定制。
另外,个别标签样式(如
1dp
附录一些AndroidTagGroup style属性定义设置字段(AndroidTagGroup库中的res/values/styles.xml文件代码):
<?xml version="1.0" encoding="utf-8"?>
match_parent
wrap_content
false
@string/add_tag
#0097a7
#0097a7
#FFFFFF
#AAAAAA
#80000000
#DE000000
#49C120
#FFFFFF
#FFFFFF
#49C120
#EDEDED
1dp
13sp
8dp
4dp
12dp
3dp
10sp
6dp
3dp
8dp
2dp
1dp
15sp
9dp
5dp
14dp
4dp
#FF3D7F
#FF3D7F
#FF3D7F
#FF3D7F
#FF3D7F
#FFFFFF
#FF3D7F
#80FFFFFF
#DEFFFFFF
#FF3D7F
#FF3D7F
#FF3D7F
#FFFFFF
#FF99B1