Android开发Android开发经验谈Android 开发技术分享

Android轻松搞定流动布局

2017-09-27  本文已影响212人  程序亦非猿580230

先上图

DA951D77A7DAD8936FEF16A2EE3B76E6.jpg

自定义一个group

public class AutoBreakViewGroup extends ViewGroup {
    private int mScreenWidth;
    private int mHorizontalSpacing;
    private int mVerticalSpacing;

    public AutoBreakViewGroup(Context context) {
        super(context);
        init();
    }

    public AutoBreakViewGroup(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);
        }
    }
}

activity调用

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

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

    private void initView() {
        mData.add("周莹少奶奶");
        mData.add("吴蔚文");
        mData.add("王世均守候一生");
        mData.add("杜明礼是个公公");
        mData.add("胡咏梅");
        mData.add("吴聘少爷");
        mData.add("图尔丹Mexico");
        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.setText(mData.get(i));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            textView.setTextColor(getResources().getColor(R.color.colorPrimary));
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setBackgroundResource(R.drawable.item_bg);
            textView.setPadding(40, 10, 40, 10);
            textView.setGravity(Gravity.CENTER);
            autoBreakViewGroup.addView(textView);
        }
    }

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wawi.AutoBreakViewGroup.MainActivity">

    <com.wawi.AutoBreakViewGroup.AutoBreakViewGroup
        android:id="@+id/autoBreakViewGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"/>

</LinearLayout>

shape

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#ffffff" />
    <stroke
        android:width="1dp"
        android:color="@color/colorPrimary" />
    <corners android:radius="5dp" />
    <padding
        android:bottom="5dp"
        android:left="12dp"
        android:right="12dp"
        android:top="5dp" />
</shape>
上一篇下一篇

猜你喜欢

热点阅读