Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 28 自定義View流式布局

28 自定義View流式布局

編輯:關於Android編程

流式布局每行的行高以本行中最高的元素作為高,如果一個元素放不下到一行時直接到第二行

FlowLayoutView

package com.qf.sxy.customview05.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * Created by sxy on 2016/9/29.
 * 流式布局   標簽
 */
public class FlowLayoutView extends ViewGroup{
    public FlowLayoutView(Context context) {
        super(context);
    }

    public FlowLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

      //或ViewGroup中所有子元素的屬性
    //否則只獲取ViewGroup 的屬性
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }


    /**
     * 測量控件本身的大小   寬和高   根據子內容獲取
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //獲取父布局的模式和尺寸
        int sizeWidth  = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        //記錄Wrap_content的寬高
        int width = 0;
        int height = 0;

        //沒一行的寬和高
        int lineWidth = 0;
        int lineHeight =0;

        //獲取子元素的數量
        int cCount = getChildCount();
        //進行遍歷子元素
        for(int i=0;igetWidth()-getPaddingLeft()-getPaddingRight()){//換行
                width = Math.max(lineWidth,childWidth);
                lineWidth = childWidth;//重新開一行  當前行的寬度
                //疊加之前的高度
                height += lineHeight;
                //重新開一行  這個新行的高度
                lineHeight = childHeight;
            }else{//不換行
                //記錄當前行的寬
                lineWidth += childWidth;
                //記錄一行中的高度
                lineHeight = Math.max(lineHeight,childHeight);
            }

            if(i ==cCount-1){//判斷是否是最後一個元素
                width = Math.max(width,lineWidth);
                height += lineHeight;
            }
            //設置ViewGrop最終寬高
            setMeasuredDimension(modeWidth==MeasureSpec.EXACTLY?sizeWidth:width+getPaddingRight()+getPaddingLeft(),
                                modeHeight==MeasureSpec.EXACTLY?sizeHeight:height+getPaddingBottom()+getPaddingTop());
        }

    }

    //記錄所有子元素   按行存取
    List> mAllViews = new ArrayList<>();
    //記錄每一行中最大的高度
    private List mLineHeight = new ArrayList<>();

    //擺放子View ???????????????
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //為了多次執行時  數據正確
        mAllViews.clear();
        mLineHeight.clear();

        //獲取父控件的寬度
        int width = getWidth();

        //記錄一行中的寬和高
        int lineWidth = 0;
        int lineHeight = 0;

        //記錄一行中的 View
        List lineView = new ArrayList<>();

        //獲取子元素的個數
        int cCount = getChildCount();
        Log.e("AAA","==AAAA=>"+cCount);
        //遍歷所有子元素
        for(int i=0;iwidth-getPaddingLeft()-getPaddingRight()){//判斷是否換行
                //記錄一行的高 和一行中的View
                mLineHeight.add(lineHeight);
                mAllViews.add(lineView);

                Log.e("AAA","==>VVV===>"+mAllViews.size());
                //讓數據重置
                lineWidth = 0;
                lineView = new ArrayList<>();
                 if (i == cCount - 1) {
                    lineView.add(child);
                }
            }else{
                //記錄一行中的寬  高  view
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight,childHeight);
                lineView.add(child);
            }



        }
//處理元素   數據以每行的形式處理
        mLineHeight.add(lineHeight);
        mAllViews.add(lineView);


        //獲取每個View的padding
        int left = getPaddingLeft();
        int top = getPaddingTop();
        //獲取有多少行
        int lineNum = mAllViews.size();
        Log.e("AAA","=BBB=>"+lineNum);
        //展示每一行
        for(int i=0;i

MainActivity

package com.qf.sxy.customview05;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.widget.TextView;

import com.qf.sxy.customview05.widget.FlowLayoutView;

public class MainActivity extends AppCompatActivity {

    private FlowLayoutView flowLayoutView;

    private String[]titils = {"sds","da","aa","fff","gggggg","hhgghg","jjhhhh","aa","fff","gggggg","hhgghg","jjhhhh","sds","da","aa","fff","gggggg","sds","da","aa","fff","gggggg"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        flowLayoutView = ((FlowLayoutView) findViewById(R.id.flowLayoutView));

        //5.fori
        for (int i = 0; i < titils.length; i++) {
//            Button btn = new Button(MainActivity.this);
//            btn.setText(titils[i]);
//            //設置Button屬性 內容包裹
//            ViewGroup.MarginLayoutParams p= new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
//            flowLayoutView.addView(btn,p);
            TextView tv = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.tv,flowLayoutView,false);
            tv.setText(titils[i]);
            flowLayoutView.addView(tv);
        }
    }
}

activity_main.xml




    

    

tv.xml





tv_shape.xml




    
    
    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved