前言:我最大的梦想,就是有一天,等老了坐在摇椅上回望一生,有故事给孩子们讲……。
相关文章:
《Android自定义控件三部曲文章索引》:http://blog.csdn.net/harvic880925/article/details/50995268
经过上篇的铺垫,这篇就开始正式开始FlowLayout的开发啦,还是先给大家上上效果:
从效果图中可以看到,底部container的布局方式应该是layout_width="match_parent",layout_height="wrap_content";
好了,废话不多说了,下面开始进入正规。
一、XML布局
从布局图中可以看到,FlowLayout中包含了很多TextView.难度不大,布局代码如下:
先定义一个style,这是为FlowLayout中的TextView定义的:
<style name="text_flag_01">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">4dp</item>
<item name="android:background">@drawable/flag_01</item>
<item name="android:textColor">#ffffff</item>
</style>
注意,注意!!!我们这里定义了margin,还记得上篇中怎么样提取Margin值吗?重写generateLayoutParams()函数。
下面看activity_main.xml的布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.harvic.myapplication.FlowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff">
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="Welcome"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="IT工程师"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="我真是可以的"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="你觉得呢"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="不要只知道挣钱"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="努力ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="I thick i can"
android:textColor="#43BBE7" />
</com.example.harvic.myapplication.FlowLayout>
</LinearLayout>
这里注意两点,FlowLayout的android:layout_width设置为"ma