您的位置:首页 > 其它

标签布局,实现添加多个button自动换行,可自定义许多属性

2016-04-04 14:05 656 查看
想要找一个放置button的布局,这个布局在添加button时能够自动换行,也能够整体居中显示


网上的很多写的能自动换行,但不能居中显示。在github上搜索了很久,最后搜到关键字“tag"

找到很多类似的布局。

下面是其中一个
https://github.com/whilu/AndroidTagView

首先添加依赖:

dependencies {
compile 'co.lujun:androidtagview:1.0.3'
}

然后在布局文件中就能添加

<co.lujun.androidtagview.TagContainerLayout
android:padding="4dp"
android:id="@+id/tag"
android:background="#6000"
android:foregroundGravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>


代码中可以这样写

tag = (TagContainerLayout) findViewById(R.id.tag);
if (tag != null) {
tag.setGravity(Gravity.CENTER);
tag.setBackgroundColor(Color.alpha(0));//整个背景色
tag.setBorderColor(Color.alpha(0));//整个容器的边框色,因为有默认值,所以我自己设为透明
init();
}

直接添加标签

private void init() {
tag.addTag("你好,世界");
tag.addTag("黄历");
tag.addTag("生物钟");
tag.addTag("不一样的世界观");
tag.addTag("瞎逛");
tag.addTag("美丽的姑娘");
tag.addTag("苦逼的程序员");
tag.addTag("代码");
tag.addTag("瞧新鲜");
tag.addTag("美食");
tag.addTag("美景");
tag.addTag("出国旅游");
tag.addTag("大学生");

}

效果:

补充说明:

tag.setTheme(ColorFactory.NONE);//必须设置这个才能设置其中一个tag




或者添加自定义布局

private void init() {
addView("text");
addView("这里");
addView("Bellion");
addView("自动换行的测试");
addView("居中显示");
addView("变化颜色");
addView("textView");
addView("背景边框");

}
private void addView(String text) {
//添加自定义布局
View inflate = LayoutInflater.from(this).inflate(R.layout.tag_item, null);
TextView textView = (TextView) inflate.findViewById(R.id.textView);
textView.setText(text);
tag.addView(inflate);
}


tag_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:padding="10dp"
android:text="#绘画"
android:textColor="#fff"
android:background="@drawable/tag_item_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


实现效果。





github上也有其他类似的控件,随你喜欢,用法类似
8d56
,主要看属性介绍
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: