流式布局

4个布局页面
1:
 

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_height="70dp"
        android:layout_width="match_parent"
        android:padding="5dp"
        android:weightSum="1">

    <EditText
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:id="@+id/edtext"
        android:background="@drawable/edit_bg"
        android:layout_weight="0.8"/>

    <Button
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:id="@+id/add"
        android:layout_weight="0.2"
        android:text="添加"/>

</LinearLayout>
    <com.myliushibuj.SelfView
        android:id="@+id/self_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.myliushibuj.SelfView>

</LinearLayout>
2:self_view.xml

<RelativeLayout 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"
    >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout_all"
    >
</LinearLayout>

    </RelativeLayout>
3:self_view_h.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="horizontal"
    tools:context=".MainActivity">


</LinearLayout>
4:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/edit_bg"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_txt"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
/*/
主显示页面

public class MainActivity extends AppCompatActivity {
    private EditText mEditText;
    private List<String> stringList=new ArrayList<>();
    private SelfView mSelfView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSelfView=findViewById(R.id.self_view);
        mEditText=findViewById(R.id.edtext);
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message=mEditText.getText().toString().trim();
                if(TextUtils.isEmpty(message)){
                    Toast.makeText(MainActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                stringList.add(message);
                mSelfView.setListData(stringList);
            }
        });
    }
}
  view 页面

public class SelfView extends RelativeLayout {
  private LinearLayout mLayout;
    public SelfView(Context context) {
        super(context);
        init(context);

    }

    public SelfView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SelfView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private Context context;
    private void init(Context context) {
        this.context=context;
        //第一步,创建垂直的LinearLayout
        View view=View.inflate(context, R.layout.self_view,null);
        //垂直方向
        mLayout= view.findViewById(R.id.layout_all);
        addView(view);
    }
    private  LinearLayout view1;
    private int position=0;
    public void setList() {
        mLayout.removeAllViews();
        //第二步,创建一个水平的LinearLayout
        view1= (LinearLayout) View.inflate(context,R.layout.self_view_h,null);



        mLayout.addView(view1);
        view1.removeAllViews();
        int len=0;
        for (int a=0;a<listData.size();a++){
            position=a;
            String data= listData.get(a);
            len+=data.length();
            if(len>22){
                view1= (LinearLayout) View.inflate(context,R.layout.self_view_h,null);
                mLayout.addView(view1);
                len=0;
            }
            final View view2=View.inflate(context,R.layout.self_view_text,null);
            final TextView textView= view2.findViewById(R.id.tv_txt);
            textView.setText(data);
            view1.addView(view2);

            LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) view2.getLayoutParams();
            params.weight=1;
            params.topMargin=10;
            params.rightMargin=10;
            params.leftMargin=10;
            view2.setLayoutParams(params);

            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,textView.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });

            //长按删除
            textView.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    view1.removeView(view2);
                    listData.remove(position);
                    setList();
                    return true;
                }
            });

        }

    }

    //传递数据
    private List<String> listData;
    public void setListData(List<String> listData) {
        this.listData=listData;
        setList();
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值