Android流式布局的简单实现

之前有看过网上好多都是写的自定义控件,感觉那样子太麻烦,自己就用线性布局加textview实现了一个,思路就是先计算一行就是一个水平的线性布局,最外层是个垂直的线性布局,然后根据子控件的长度动态添加即可。
这里我还做了一件事,就是当不能完全显示的时候,我计算了水平间距,这样看起来舒服点。
效果图:
Android流式布局的简单实现_第1张图片

然后是主要的代码:MyLayout.java

package com.example.buttontext;

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

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyLayout extends LinearLayout{
private Context context;
private List list_i;//记录每一行的textview的个数
private Listlist_t;//textview
private List list_m;//记录每一行的左边距
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
list_i = new ArrayList();
list_m = new ArrayList();
list_t = new ArrayList();
}

public void settext(List<String> list) {
//  measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
    int singleWith = getScreenWidth(context);
    int totalWith = 0;
    int endIndex = 0;
    int temp = 0;
    for (int i = endIndex; i < list.size(); i++) {
        TextView tv = (TextView) LayoutInflater.from(context).inflate(R.layout.item_textview, null);
        list_t.add(tv);
        tv.setText(list.get(i));
        tv.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        totalWith = tv.getMeasuredWidth() + totalWith;
        if (totalWith > singleWith) {
            int n = i-endIndex;
            endIndex = i;
            list_i.add(endIndex);
            int m = (singleWith-totalWith+tv.getMeasuredWidth())/(n+1);
            list_m.add(m);
            totalWith = tv.getMeasuredWidth();              
        }
    }

    for (int i = 0; i < list_i.size(); i++) {
        LinearLayout ll = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.mylayoutitem, null);
        for (int j = temp; j < list_i.get(i); j++) {            
            ll.addView(list_t.get(j));
            LinearLayout.LayoutParams layoutParams = (LayoutParams) list_t.get(j).getLayoutParams();
            layoutParams.setMargins(list_m.get(i), 0,0,0);
            list_t.get(j).setLayoutParams(layoutParams);
        }
        addView(ll);

        temp = list_i.get(i);
    }
    if (list_i.size() == 0) {
        return;
    }
    LinearLayout ll = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.mylayoutitem, null);
    for (int i = list_i.get(list_i.size() - 1); i < list.size(); i++) {

        ll.addView(list_t.get(i));

    }
    addView(ll);
}

/**
 * 获取屏幕的宽度px
 */
private int getScreenWidth(Context context) {
    DisplayMetrics metric = new DisplayMetrics();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getMetrics(metric);
    return metric.widthPixels;
}

}

最后是下载链接:
http://download.csdn.net/detail/feiyang877647044/9487079

你可能感兴趣的:(流式布局)