欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义ViewGroup实现换行

程序员文章站 2022-06-08 17:34:42
...

1.AutoViewGroup

package com.example.yangdechengapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

public class AutoViewGroup extends ViewGroup {
    private int mScreenWidth;//屏幕的宽度
    private int mHorizontalSpacing;//横向的自view的间距
    private int mVerticalSpacing;//行间距

    public AutoViewGroup(Context context) {
        super(context);
    }

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

    public void setSpacing(int horizontalSpacing, int verticalSpacing) {
        mHorizontalSpacing = horizontalSpacing;
        mVerticalSpacing = verticalSpacing;
    }

    private void init() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        mScreenWidth = dm.widthPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //也可以根据根据孩子的大小计算出自身的宽高

        ////根据自身的宽度约束子控件宽度
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //设置自身宽度(默认的宽高是充满屏幕的)
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int mTotalHeight = 0;
        int mTotalWidth = 0;
        int mTempHeight = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            int measureHeight = childView.getMeasuredHeight();
            int measuredWidth = childView.getMeasuredWidth();
            mTempHeight = (measureHeight > mTempHeight) ? measureHeight : mTempHeight;
            if ((measuredWidth + mTotalWidth + mHorizontalSpacing) > mScreenWidth) {
                mTotalWidth = 0;
                mTotalHeight += (mTempHeight + mVerticalSpacing);
                mTempHeight = 0;
            }
            childView.layout(mTotalWidth + mHorizontalSpacing, mTotalHeight, measuredWidth + mTotalWidth + mHorizontalSpacing, mTotalHeight + measureHeight);
            mTotalWidth += (measuredWidth + mHorizontalSpacing);
            int a=0;
            String x="";


        }
    }
}

2.Activity

package com.example.yangdechengapplication.customview;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yangdechengapplication.R;
import com.example.yangdechengapplication.view.AutoViewGroup;

import java.util.ArrayList;


public class CustomviewActivity extends AppCompatActivity {
    private ArrayList<String> mData = new ArrayList<>();
    private Context mContext;
    private AutoViewGroup autoBreakViewGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_customview_test);
        autoBreakViewGroup = (AutoViewGroup) findViewById(R.id.autoBreakViewGroup);
        autoBreakViewGroup.setSpacing(20, 20);
        initView();
    }

    private void initView() {
        mData.add("猪八戒");
        mData.add("刘备");
        mData.add("无能");
        mData.add("旋风少女");
        mData.add("孙悟空");
        mData.add("拉玛西亚");

        for (int i = 0; i < mData.size(); i++) {
            TextView textView = new TextView(mContext);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Toast在其他线程中调用显示", Toast.LENGTH_SHORT).show();

                }

            });
            textView.setText(mData.get(i));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            textView.setTextColor(Color.BLACK);
            textView.setLayoutParams(new
                    ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setBackgroundResource(R.drawable.btn_radius_blue_bg);
            textView.setPadding(20, 10, 20, 10);
            textView.setGravity(Gravity.CENTER);
            autoBreakViewGroup.addView(textView);
        }
    }

}


3.XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.yangdechengapplication"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.yangdechengapplication.view.CustomTitleView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:titleText="3712"
        android:padding="10dp"
        custom:titleTextColor="#ff0000"
        android:layout_centerInParent="true"
        custom:titleTextSize="40sp"  />

</RelativeLayout>