自定义控件三部曲视图篇(二)——FlowLayout自适应容器实现

本文详细介绍了自定义控件FlowLayout的实现,通过XML布局、提取margin和重写onMeasure()、onLayout()方法,实现容器的自适应布局。在onMeasure()中计算每个子控件的宽高并判断换行,而在onLayout()中布局所有子控件。提供源码下载,适合Android开发者学习自定义ViewGroup。
摘要由CSDN通过智能技术生成

前言:我最大的梦想,就是有一天,等老了坐在摇椅上回望一生,有故事给孩子们讲……。

 

 

相关文章:

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

  • 32
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值