标签布局,实现添加多个button自动换行,可自定义许多属性
2016-04-04 14:05
656 查看
想要找一个放置button的布局,这个布局在添加button时能够自动换行,也能够整体居中显示
网上的很多写的能自动换行,但不能居中显示。在github上搜索了很久,最后搜到关键字“tag"
找到很多类似的布局。
下面是其中一个
https://github.com/whilu/AndroidTagView
首先添加依赖:
然后在布局文件中就能添加
代码中可以这样写
直接添加标签
效果:
补充说明:
或者添加自定义布局
tag_item.xml
实现效果。
github上也有其他类似的控件,随你喜欢,用法类似
8d56
,主要看属性介绍
网上的很多写的能自动换行,但不能居中显示。在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
,主要看属性介绍
相关文章推荐
- 杭电1021Fibonacci Again
- 【jQuery源码】html,text,val
- 数据结构二叉树遍历之中序遍历
- Linux ALSA声卡驱动之七:ASoC架构中的Codec
- 左旋转字符串m位
- [Debug记录]getFileExtensionFromUrl 没有值 null
- 迭代的模块itertools
- 迭代的模块itertools
- 迭代的模块itertools
- CSP考试 2013年12月第3题 最大矩形 C语言实现
- Vsftpd服务搭建
- 110. Balanced Binary Tree
- HDU-1257 最少拦截系统 ( 贪心 )
- Linux ALSA声卡驱动之六:ASoC架构中的Machine
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
- linux server, windows SQL server 配置
- 设计模式C++工厂模式
- C++的tie()函数
- UVA 11090(SPFA判负环)
- Linux源代码分析工具链