1. 程式人生 > >流式佈局(不完整)

流式佈局(不完整)

FlowLayout類

package com.bw.treasure.makeoview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FloatLayout extends LinearLayout {

private int widthSccren;
private int heightSccren;



public FloatLayout(Context context) {
    this(context,null);
}

public FloatLayout(Context context, AttributeSet attrs) {
    this(context, attrs,-1);
}

public FloatLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //把方向改為垂直
    setOrientation(VERTICAL);
    //獲取螢幕寬高
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    widthSccren=displayMetrics.widthPixels;
    //注意高度不需要
    heightSccren=displayMetrics.heightPixels;

}


public void setAdapter(String[] data){
    //去掉所有的子view
    removeAllViews();
   innerSetAdapter(data);
}

public void add(String[] data) {
    innerSetAdapter(data);
}

private void innerSetAdapter(String[] data){
    LinearLayout linearLayout = iniHorL();

    for (int i = 0; i < data.length; i++) {
        String temp = data[i];
        int childCount = linearLayout.getChildCount();
        int totalWith=0;
        for (int j = 0; j <childCount ; j++) {
            int measuredWidth = linearLayout.getMeasuredWidth();
            TextView childAt = (TextView) linearLayout.getChildAt(j);
            //一定要呼叫一次測量方法
            childAt.measure(getMeasuredWidth(),getMeasuredHeight());

            int width = childAt.getMeasuredWidth();
            LinearLayout.LayoutParams layoutParams = (LayoutParams) childAt.getLayoutParams();

            totalWith+=width+layoutParams.leftMargin;
        }
        final TextView textView = iniTextView();
        LinearLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity=Gravity.CENTER;
        params.leftMargin=10;
        params.topMargin=10;
        params.bottomMargin=10;
        textView.setText(temp);

        textView.measure(getMeasuredWidth(),getMeasuredHeight());
        int measuredWidth = textView.getMeasuredWidth();
        if (widthSccren>= totalWith+ measuredWidth+params.leftMargin) {
            linearLayout.addView(textView,params);
        }else {
            //這裡注意不能新宣告變數
            linearLayout = iniHorL();
            linearLayout.addView(textView,params);
        }

    }
}
private LinearLayout iniHorL(){
    LinearLayout linearLayout = new LinearLayout(getContext());
    linearLayout.setOrientation(HORIZONTAL);
    LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    this.addView(linearLayout,params);
    return linearLayout;
}

private TextView iniTextView(){
    TextView textView = new TextView(getContext());
    textView.setTextColor(Color.parseColor("#ff0000"));
    textView.setTextSize(20);
    return textView;
}

}

MainActivity檔案
package com.bw.treasure.makeoview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
private String[] desc = new String[]{“優秀”,“邯鄲學步”,“優秀”,“邯鄲學步”,“東施效顰”,“東施效顰”,“優秀”,“邯鄲學步”,“東施效顰”,
“優秀”,“邯鄲學步”,“東施效顰”,“優秀”,“優秀”,“邯鄲學步”,“東施效顰”,“邯鄲學步”,“東施效顰”,“優秀”,“邯鄲學步”,“東施效顰”,“優秀”,“邯鄲學步”,“東施效顰”};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatLayout viewById = findViewById(R.id.floatlayout);

    viewById.setAdapter(desc);
    viewById.add(desc);


}

}