在进行Android开发的时候,比如我们添加view到LineLayout中,如果是水平布局可能会一直水平添加导致产生“超出屏幕”的类似现象,所以就需要进行换行操作,下面是进行换行的代码,可以直接使用~

         

import java.util.ArrayList;import java.util.List;import android.annotation.SuppressLint;import android.app.ActionBar.LayoutParams;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.View.MeasureSpec;import android.view.ViewGroup;public class CustomLayout extends ViewGroup {    private static final String TAG = "CustomLayout";  public CustomLayout(Context context) {      super(context);  }  public CustomLayout(Context context, AttributeSet attrs) {      this(context, attrs, 0);  }  public CustomLayout(Context context, AttributeSet attrs, int defStyle) {      super(context, attrs, defStyle);  }  /**   * 要求所有的孩子测量自己的大小,然后根据这些孩子的大小完成自己的尺寸测量   */  @SuppressLint("NewApi") @Override  protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {      // 计算出所有的childView的宽和高       measureChildren(widthMeasureSpec, heightMeasureSpec);       //测量并保存layout的宽高(使用getDefaultSize时,wrap_content和match_perent都是填充屏幕)      //稍后会重新写这个方法,能达到wrap_content的效果      setMeasuredDimension( getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),              getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));  }  /**   * 为所有的子控件摆放位置.   */  @Override  protected void onLayout( boolean changed, int left, int top, int right, int bottom) {      final int count = getChildCount();      int childMeasureWidth = 0;      int childMeasureHeight = 0;      int layoutWidth = 0;    // 容器已经占据的宽度      int layoutHeight = 0;   // 容器已经占据的宽度      int maxChildHeight = 0; //一行中子控件最高的高度,用于决定下一行高度应该在目前基础上累加多少      for(int i = 0; imaxChildHeight){                maxChildHeight = childMeasureHeight;          }           //确定子控件的位置,四个参数分别代表(左上右下)点的坐标值          child.layout(left, top, right, bottom);      }  }}

 

更多相关文章

  1. (ios实战)实现类似于android 的toast控件
  2. [Android] 开发资料收集:视图、组件、自定义控件
  3. Android(安卓)Studio图片框
  4. Android初级教程_获取Android控件的宽和高 .
  5. Android(安卓)如何通过menu id来得到menu item 控件
  6. Android中获取屏幕信息的几种方式
  7. Android(安卓)实用项目链接
  8. android取得系统高度,标题栏和状态高度
  9. Android提高之多方向抽屉实现方法

随机推荐

  1. RotateAnimation类:旋转变化动画类
  2. Android(安卓)Cannot add task 'reportSo
  3. Android开发规范:JavaDoc
  4. Android(安卓)4.0:Unable to find a 'user
  5. 一起学android之如何获取手机程序列表以
  6. Android(安卓)Studio创建RelativeLayout
  7. android IllegalStateException
  8. Android(安卓)内存管理机制
  9. 【Android(安卓)Developers Training】 1
  10. Android(安卓)View的滚动scroll 、androi