1 创建一个类 我的叫 IViewi
package com.example.lenovo.month021025; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * */ public class IViewi extends ViewGroup{ public IViewi(Context context) { this(context,null); } public IViewi(Context context, AttributeSet attrs) { this(context, attrs,0); } public IViewi(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onLayout(boolean b, int i, int i1, int i2, int i3) { measureChildren(0,0); int totalw = 0; int totalh = 0; for (int i4 = 0; i4<getChildCount(); i4++){ View view = getChildAt(i4); if (totalw+view.getMeasuredWidth()>=getMeasuredWidth()){ totalw =0; totalh+=view.getMeasuredHeight(); } // view.layout(totalw,totalh,totalw+view.getMeasuredWidth(),totalh+view.getMeasuredHeight()); view.layout( totalw, totalh, totalw + view.getMeasuredWidth(), totalh + view.getMeasuredHeight()); totalw+=view.getMeasuredWidth(); } } }
(点击搜索框 跳转到这个创建的Activity)
2 创建一个Activity 我的叫LsbjActivity
package com.example.lenovo.month021025; 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 LsbjActivity extends AppCompatActivity { private IViewi iViewi; private EditText edtLsbj; private Button btnShou; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lsbj); iViewi = findViewById(R.id.lsbj); edtLsbj = findViewById(R.id.edt_lsbj); btnShou = findViewById(R.id.btn_shou); btnShou.setOnClickListener(new View.OnClickListener() { private TextView textView; @Override public void onClick(View view) { textView = new TextView(LsbjActivity.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 = edtLsbj.getText().toString(); textView.setText(ed_name); iViewi.addView(textView); } }); } }
3 Activity布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/edt_lsbj" android:layout_weight="9" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_shou" android:text="搜索" android:layout_weight="2" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> //这是我的项目包名 <com.example.lenovo.month021025.IViewi android:id="@+id/lsbj" android:layout_width="match_parent" android:layout_height="wrap_content"></com.example.lenovo.month021025.IViewi> </LinearLayout>