热门标签流式布局的实现--防网易云音乐热门标签

详情请看大神http://blog.csdn.net/jdsjlzx/article/details/45042081?ref=myread

热门标签流式布局的实现

  1. 自定义热门标签的ViewGroup实现

      根据上面的技术分析,自定义类继承于ViewGroup,并重写 onMeasure和onLayout等方法。具体实现代码如下:

    package com.czm.flowlayout;  

    import java.util.ArrayList;  
    import java.util.List;  

    import android.content.Context;  
    import android.util.AttributeSet;  
    import android.view.View;  
    import android.view.ViewGroup;  
    /**  
     *   
     * @author caizhiming  
     * @created on 2015-4-13  
     */  
    public class XCFlowLayout extends ViewGroup{  

        //存储所有子View  
        private List<List<View>> mAllChildViews = new ArrayList<>();  
        //每一行的高度  
        private List<Integer> mLineHeight = new ArrayList<>();  

        public XCFlowLayout(Context context) {  
            this(context, null);  
            // TODO Auto-generated constructor stub  
        }  
        public XCFlowLayout(Context context, AttributeSet attrs) {  
            this(context, attrs, 0);  
            // TODO Auto-generated constructor stub  
        }  
        public XCFlowLayout(Context context, AttributeSet attrs, int defStyle) {  
            super(context, attrs, defStyle);  
            // TODO Auto-generated constructor stub  
        }  
        @Override  
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
            // TODO Auto-generated method stub  

            //父控件传进来的宽度和高度以及对应的测量模式  
            int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);  
            int modeWidth = MeasureSpec.getMode(widthMeasureSpec);  
            int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);  
            int modeHeight = MeasureSpec.getMode(heightMeasureSpec);  

            //如果当前ViewGroup的宽高为wrap_content的情况  
            int width = 0;//自己测量的 宽度  
            int height = 0;//自己测量的高度  
            //记录每一行的宽度和高度  
            int lineWidth = 0;  
            int lineHeight = 0;  

            //获取子view的个数  
            int childCount = getChildCount();  
            for(int i = 0;i < childCount; i ++){  
                View child = getChildAt(i);  
                //测量子View的宽和高  
                measureChild(child, widthMeasureSpec, heightMeasureSpec);  
                //得到LayoutParams  
                MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();  
                //子View占据的宽度  
                int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;  
                //子View占据的高度  
                int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;  
                //换行时候  
                if(lineWidth + childWidth > sizeWidth){  
                    //对比得到最大的宽度  
                    width = Math.max(width, lineWidth);  
                    //重置lineWidth  
                    lineWidth = childWidth;  
                    //记录行高  
                    height += lineHeight;  
                    lineHeight = childHeight;  
                }else{//不换行情况  
                    //叠加行宽  
                    lineWidth += childWidth;  
                    //得到最大行高  
                    lineHeight = Math.max(lineHeight, childHeight);  
                }  
                //处理最后一个子View的情况  
                if(i == childCount -1){  
                    width = Math.max(width, lineWidth);  
                    height += lineHeight;  
                }  
            }  
            //wrap_content  
            setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,  
                    modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);  
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
        }  

        @Override  
        protected void onLayout(boolean changed, int l, int t, int r, int b) {  
            // TODO Auto-generated method stub  
            mAllChildViews.clear();  
            mLineHeight.clear();  
            //获取当前ViewGroup的宽度  
            int width = getWidth();  

            int lineWidth = 0;  
            int lineHeight = 0;  
            //记录当前行的view  
            List<View> lineViews = new ArrayList<View>();  
            int childCount = getChildCount();  
            for(int i = 0;i < childCount; i ++){  
                View child = getChildAt(i);  
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();  
                int childWidth = child.getMeasuredWidth();  
                int childHeight = child.getMeasuredHeight();  

                //如果需要换行  
                if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){  
                    //记录LineHeight  
                    mLineHeight.add(lineHeight);  
                    //记录当前行的Views  
                    mAllChildViews.add(lineViews);  
                    //重置行的宽高  
                    lineWidth = 0;  
                    lineHeight = childHeight + lp.topMargin + lp.bottomMargin;  
                    //重置view的集合  
                    lineViews = new ArrayList();  
                }  
                lineWidth += childWidth + lp.leftMargin + lp.rightMargin;  
                lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);  
                lineViews.add(child);  
            }  
            //处理最后一行  
            mLineHeight.add(lineHeight);  
            mAllChildViews.add(lineViews);  

            //设置子View的位置  
            int left = 0;  
            int top = 0;  
            //获取行数  
            int lineCount = mAllChildViews.size();  
            for(int i = 0; i < lineCount; i ++){  
                //当前行的views和高度  
                lineViews = mAllChildViews.get(i);  
                lineHeight = mLineHeight.get(i);  
                for(int j = 0; j < lineViews.size(); j ++){  
                    View child = lineViews.get(j);  
                    //判断是否显示  
                    if(child.getVisibility() == View.GONE){  
                        continue;  
                    }  
                    MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();  
                    int cLeft = left + lp.leftMargin;  
                    int cTop = top + lp.topMargin;  
                    int cRight = cLeft + child.getMeasuredWidth();  
                    int cBottom = cTop + child.getMeasuredHeight();  
                    //进行子View进行布局  
                    child.layout(cLeft, cTop, cRight, cBottom);  
                    left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;  
                }  
                left = 0;  
                top += lineHeight;  
            }  

        }  
        /**  
         * 与当前ViewGroup对应的LayoutParams  
         */  
        @Override  
        public LayoutParams generateLayoutParams(AttributeSet attrs) {  
            // TODO Auto-generated method stub  

            return new MarginLayoutParams(getContext(), attrs);  
        }  
    }  

2,相关布局的文件引用

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:id="@+id/container"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  

        <com.czm.flowlayout.XCFlowLayout  
            android:id="@+id/flowlayout"  
            android:layout_width="match_parent"  
            android:layout_height="match_parent" >  

        </com.czm.flowlayout.XCFlowLayout>  

    </RelativeLayout>  
可以给个Shape图形背景更美观

三、使用该自定义布局控件类

最后,如何使用该自定义的热门标签控件类呢?很简单,请看下面实例代码:

    package com.czm.flowlayout;  

    import android.app.Activity;  
    import android.graphics.Color;  
    import android.os.Bundle;  
    import android.view.ViewGroup.LayoutParams;  
    import android.view.ViewGroup.MarginLayoutParams;  
    import android.widget.TextView;  
    /**  
     *   
     * @author caizhiming  
     * @created on 2015-4-13  
     */  
    public class MainActivity extends Activity {  

        private String mNames[] = {  
                "welcome","android","TextView",  
                "apple","jamy","kobe bryant",  
                "jordan","layout","viewgroup",  
                "margin","padding","text",  
                "name","type","search","logcat"  
        };  
        private XCFlowLayout mFlowLayout;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  

            initChildViews();  

        }  
        private void initChildViews() {  
            // TODO Auto-generated method stub  
            mFlowLayout = (XCFlowLayout) findViewById(R.id.flowlayout);  
            MarginLayoutParams lp = new MarginLayoutParams(  
                    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
            lp.leftMargin = 5;  
            lp.rightMargin = 5;  
            lp.topMargin = 5;  
            lp.bottomMargin = 5;  
            for(int i = 0; i < mNames.length; i ++){  
                TextView view = new TextView(this);  
                view.setText(mNames[i]);  
                view.setTextColor(Color.WHITE);  
                view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));  
                mFlowLayout.addView(view,lp);  
            }  
        }  

    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 流式布局算法是用于在网页中布局内容的常用方法。它的基本原理是将内容分成一个个小块,然后按照一定的规则将这些小块排列在网页上。 流式布局算法的具体实现方式可能有所差异,但通常会包括以下步骤: 1. 设置布局的容器和内容的小块。在 HTML 中,容器可以是 div 元素,而小块则是 p 元素或其他元素。 2. 设置布局的样式。在 CSS 中,可以使用 display: flex 或 display: grid 等属性来设置流式布局。 3. 设置布局的方向。在 CSS 中,可以使用 flex-direction 或 grid-template-columns 等属性来设置布局的方向。 4. 设置布局的间隔。在 CSS 中,可以使用 margin 或 padding 等属性来设置布局的间隔。 5. 设置布局的尺寸。在 CSS 中,可以使用 width 或 height 等属性来设置布局的尺寸。 6. 调整布局的其他属性。根据实际需要,还可以调整布局的其他属性,如对齐方式、换行方式等。 以上是流式布局算法的基本实现步骤,但是具体的实现方法可能会因为不同的需求而有所差异。 ### 回答2: 流式布局算法是一种用于处理具有不确定宽度和高度的网页元素的布局技术。其主要目的是将元素自动适应容器,并在容器宽度不足时自动换行。下面是流式布局算法的具体实现过程: 1. 确定容器的宽度:获取容器的宽度,通常是通过CSS样式指定的。 2. 确定元素的宽度:根据元素的内容、样式和布局规则来确定元素的宽度。可以使用百分比、固定像素等不同的单位。 3. 计算元素的位置:根据元素的宽度和容器的宽度,计算出元素在容器中的位置。通常情况下,第一个元素位于容器的左上角,后续元素则依次向右排列。 4. 判断是否需要换行:在每个元素放置后,判断容器的宽度是否足够容纳下一个元素。如果不够,则需要进行换行操作。 5. 计算换行后的位置:在需要换行时,将下一个元素放置到下一行的起始位置。根据元素的宽度和容器的宽度,确定元素在新行中的位置。 6. 重复步骤3到步骤5,直到所有元素都被放置到容器中。 流式布局算法的核心是计算容器的宽度和确定元素的位置。通过不断地计算和判断,保证元素能够按照指定的布局规则进行排列,并自动适应容器的宽度。这种布局方式广泛应用于响应式设计中的网页布局,可以实现在不同尺寸的设备上都能够显示出良好的效果。 ### 回答3: 流式布局算法是一种网页设计中常用的布局算法,其目的是实现页面元素的自适应排列。该算法的具体实现可以分为以下几个步骤: 1. 对元素进行排序:首先,根据元素的顺序决定它们在页面上的排列顺序。通常情况下,从左到右、从上到下的顺序是流式布局的基本规则。 2. 计算元素宽度:根据网页容器的宽度和元素所占的比例,计算出每个元素的实际宽度。可以使用百分比、固定像素值或其他单位来确定宽度。如果元素的宽度超过了容器的宽度,则可能需要对元素进行换行处理。 3. 处理元素换行:在进行元素换行时,需要注意每行元素的高度和间距情况,并适当调整元素的位置。若元素的高度不一致,则需要对齐元素的底部或顶部。换行的具体策略可以根据设计需求来决定。 4. 处理元素间距:在元素排列的过程中,需要考虑元素间的间距,以保证整体布局的美观性。可以通过设置元素之间的外边距、内边距或添加分隔线等方式来实现。 5. 响应式布局:为了适应不同设备和屏幕大小的需求,流式布局算法还需要考虑响应式设计。根据屏幕宽度的变化,动态调整元素的排列方式和宽度,以达到更好的用户体验。 总的来说,流式布局算法的实现可以根据具体的需求和设计风格来进行调整和优化。通过合理的设置元素宽度、行高和间距,可以实现一个动态、自适应的布局效果,使得网页在不同分辨率下都能够呈现出较好的展示效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值