LSBJ

 

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>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值