首先:EdText和TextView的组合控件就不写了
直接开始重写FrameLayout
public class LayoutViewTwo extends FrameLayout {
private int dimension;
public LayoutViewTwo(Context context) {
super(context);
}
public LayoutViewTwo(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public LayoutViewTwo(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LayoutView);
dimension = (int) typedArray.getDimension(R.styleable.LayoutView_layoutSpace, 0);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int width = this.getWidth();
int height = 0;
int rows = 0;
int hwidth = 0;
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
childAt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
}
});
childAt.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
return true;
}
});
int atWidth = childAt.getWidth();
int atHeight = childAt.getHeight();
hwidth =hwidth + atWidth +dimension;
if (hwidth>width){
rows++;
hwidth = atWidth+dimension;
}
childAt.layout(hwidth-atWidth,rows*atHeight+(rows+1)+dimension,hwidth,(rows+1)*atHeight+(rows+1)+dimension);
}
}
public void addTextView(String textView){
TextView view = (TextView) inflate(getContext(), R.layout.text_item, null);
view.setText(textView+"");
LayoutParams layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.HORIZONTAL);
view.setLayoutParams(layoutParams);
addView(view);
}
}
mainactivity类:
public class Main3Activity extends AppCompatActivity {
private SearchViewTwo search;
private LayoutViewTwo layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
search = findViewById(R.id.search_view);
layout = findViewById(R.id.layout_two);
initData();
}
private void initData() {
search.setCallBack(new SearchViewTwo.SearchCallBack() {
@Override
public void TextCallBack(String text) {
layout.addTextView(text);
}
});
}
}
mainactivity的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="match_parent"
tools:context=".Main3Activity"
android:orientation="vertical"
>
<com.text.liushi.view.SearchViewTwo
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="50dp">
</com.text.liushi.view.SearchViewTwo>
<com.text.liushi.view.LayoutViewTwo
android:id="@+id/layout_two"
android:layout_width="match_parent"
android:layout_height="500dp">
</com.text.liushi.view.LayoutViewTwo>
自定义属性
<resources>
<declare-styleable name="FlayoutView">
<attr name="sou_spe" format="dimension"/>
<attr name="sou_color" format="color"/>
</declare-styleable>
text_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:background="@color/colorAccent"
android:text="趣闻"
>
</TextView>
是不是简单的一匹