这是自定义view
package com.example.abnerming.viewapplication.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.abnerming.viewapplication.R;
import java.util.ArrayList;
import java.util.List;
//自定义流失布局
public class SelfView extends RelativeLayout{
private LinearLayout mLayoutV;
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;
View view=View.inflate(context,R.layout.self_view,null);
mLayoutV=(LinearLayout)view.findViewById(R.id.layout_all);
addView(view);
}
private void setList(List<String> arrayList) {
mLayoutV.removeAllViews();
LinearLayout view=(LinearLayout) View.inflate(context,R.layout.self_view_h,null);
mLayoutV.addView(view);
int len=0;
view.removeAllViews();
for (int a=0;a<arrayList.size();a++){
String msg=arrayList.get(a);
len+=msg.length();
if(len>22){
view=(LinearLayout) View.inflate(context,R.layout.self_view_h,null);
mLayoutV.addView(view);
len=0;
}
View viewTxt=View.inflate(context,R.layout.self_view_text,null);
TextView textView=(TextView) viewTxt.findViewById(R.id.tv_txt);
textView.setText(msg);
view.addView(viewTxt);
LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) viewTxt.getLayoutParams();
params.weight=1;
params.leftMargin=10;
params.rightMargin=10;
params.topMargin=10;
viewTxt.setLayoutParams(params);
}
}
//传递数据
public void setListData(List<String> listData) {
if(!listData.isEmpty()){
setList(listData);
}
}
}
2.main
package com.example.abnerming.viewapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.abnerming.viewapplication.view.SelfView;
import java.util.ArrayList;
import java.util.List;
//流式布局
public class SelfActivity 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_self);
mSelfView=(SelfView)findViewById(R.id.self_view);
mEditText=(EditText)findViewById(R.id.edtext);
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message=mEditText.getText().toString().trim();
if(TextUtils.isEmpty(message)){
Toast.makeText(SelfActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
return;
}
stringList.add(message);
mSelfView.setListData(stringList);
}
});
}
}
3.布局
1.主布局
<?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="match_parent"
android:orientation="vertical"
tools:context=".SelfActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:weightSum="1"
android:padding="5dp"
>
<EditText
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="match_parent"
android:background="@drawable/edit_bg"
android:id="@+id/edtext"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:text="添加"
android:id="@+id/add"
/>
</LinearLayout>
<com.example.abnerming.viewapplication.view.SelfView
android:id="@+id/self_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2.itm1
<?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="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout_all"
>
</LinearLayout>
</RelativeLayout>
3.itm2
<?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.itm3
<?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>