乞丐版流式布局的简单实现

效果图

首先: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>

是不是简单的一匹

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值