流式布局
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<com.bw.qgs.liu2.LiuShi
android:id="@+id/liu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</android.support.constraint.ConstraintLayout>
public class LiuShi extends LinearLayout {
private int mHeightPixels;
private int mWidthPixels;
public LiuShi(Context context) {
this(context,null);
}
public LiuShi(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public LiuShi(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//改为垂直状态
setOrientation(VERTICAL);
//获取屏幕宽高
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
mWidthPixels = displayMetrics.widthPixels;
//高度可以省略不计
mHeightPixels = displayMetrics.heightPixels;
}
public void setAdapter(String[] data){
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;
}
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(mWidthPixels>=totalWith+measuredWidth+params.leftMargin){
linearLayout.addView(textView,params);
}else{
linearLayout = iniHorl();
linearLayout.addView(textView,params);
}
}
}
private TextView iniTextView() {
TextView textView = new TextView(getContext());
textView.setTextColor(Color.RED);
textView.setTextSize(20);
return textView;
}
private LinearLayout iniHorl() {
LinearLayout linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(HORIZONTAL);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
this.addView(linearLayout,layoutParams);
return linearLayout;
}
}
public class MainActivity extends AppCompatActivity {
private String[] desc = new String[]{"优秀","邯郸学步","优秀","邯郸学步","东施效颦","东施效颦","优秀","邯郸学步","东施效颦",
"优秀","邯郸学步","东施效颦","优秀","优秀","邯郸学步","东施效颦","邯郸学步","东施效颦","优秀","邯郸学步","东施效颦","优秀","邯郸学步","东施效颦"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LiuShi liu = findViewById(R.id.liu);
liu.setAdapter(desc);
liu.add(desc);
}
}