1.继承FrameLayout;
public class FolowActivity extends FrameLayout {
public FolowActivity(Context context) {
super(context);
getSelect();
}
public FolowActivity(Context context, AttributeSet attrs) {
super(context, attrs);
getSelect();
}
public FolowActivity(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getSelect();
}
public void getSelect(){
Dao dao = new Dao(getContext());
Cursor select = dao.select("user", null, null, null, null, null, null);
if (select.moveToFirst()){
do {
String name = select.getString(select.getColumnIndex("name"));
View view = View.inflate(getContext(),R.layout.item,null);
TextView textView = view.findViewById(R.id.mTV);
textView.setText(name);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
addView(textView);
}
while (select.moveToNext());
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//屏幕宽度
int width = getWidth();
//默认行数
int row = 0;
//子控件左边的间距
int diswidth = 20;
//for循环
for (int i = 0; i < getChildCount(); i++) {
//查找子控件
View view = getChildAt(i);
//获取子控件的宽高
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
//判断
if (diswidth+viewWidth>width){
row++;
diswidth = 20;
}
view.layout(diswidth,row*viewHeight+row*20,diswidth+viewWidth,row*viewHeight+row*20+viewHeight);
diswidth += viewWidth+20;
}
}
}