LiuShiBuJu-鑫鑫

xiaoxiao2025-07-11  9

//或者说直接for      

for (int i = 0; i < 20; i++) {             TextView txt = new TextView(this);             txt.setText("第" + i + "条数据");             txt.setBackgroundResource(R.drawable.bg);             ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,                     ViewGroup.LayoutParams.WRAP_CONTENT);             txt.setPadding(30, 30, 30, 30);             layoutParams.setMargins(100, 100, 100, 100);             txt.setLayoutParams(layoutParams);             fl.addView(txt);         }

 

package com.example.dsover; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * Created by 红鼻子小黑 on 2018/10/25. */ public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { this(context,null); } public FlowLayout(Context context, AttributeSet attrs) { this(context, attrs,0); } public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { measureChildren(0,0); int totalWidth = 0; int totalHeight = 0; for (int i = 0 ; i<getChildCount();i++){ View view = getChildAt(i); if (totalWidth + view.getMeasuredWidth()>=getMeasuredWidth()){ totalWidth = 0; totalHeight += view.getMeasuredHeight(); } view.layout(totalWidth, totalHeight, totalWidth+view.getMeasuredWidth(), totalHeight+view.getMeasuredHeight()); totalWidth += view.getMeasuredWidth(); } } }

//记这先按搜索跳转,布局就是上面的类所写的

package com.example.dsover; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class LiushiActivity extends AppCompatActivity { private com.example.dsover.FlowLayout flowLayout; private EditText editText; private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_liushi); editText = findViewById(R.id.ed_liu); button = findViewById(R.id.btn_liu); flowLayout = findViewById(R.id.lius); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView = new TextView(LiushiActivity.this); ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setPadding(10, 10, 10, 10); layoutParams.setMargins(100, 100, 100, 100); textView.setLayoutParams(layoutParams); String ed_name = editText.getText().toString(); textView.setText(ed_name); flowLayout.addView(textView); } }); } }
转载请注明原文地址: https://www.6miu.com/read-5032896.html

最新回复(0)