MeasureUtil工具类:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.GridView;
import android.widget.ListView;
 
/**
 * 常用的测量功能
 */
public class MeasureUtil {
 
	private MeasureUtil(){}
	
	/**
	 * 获取控件的测量高度
	 * @param view	控件
	 * @return	返回测量高度(MeasuredHeight)
	 */
	public static int getMeasuredHeight(View view) {
		if (view == null) {
			throw new IllegalArgumentException("view is null");
		}
		view.measure(0, 0);
		return view.getMeasuredHeight();
	}
	
	/**
     * 控件的高度
     * @param view	控件View
     * @return	返回控件的高度
     */
    public static int getHeight(View view){
    	if(view == null){
    		throw new IllegalArgumentException("view is null");
    	}
    	
    	view.measure(0, 0);
        return view.getHeight();
    }
 
    /**
     * 获取控件的测量宽度
     * @param view	控件
     * @return	返回控件的测量宽度
     */
    public static int getMeasuredWidth(View view){
    	if(view == null){
    		throw new IllegalArgumentException("view is null");
    	}
    	
    	view.measure(0, 0);
        return view.getMeasuredWidth();
    }
    
    /**
     * 获取控件的宽度
     * @param view	控件
     * @return	返回控件的宽度
     */
    public static int getWidth(View view){
    	if(view == null){
    		throw new IllegalArgumentException("view is null");
    	}
    	
    	view.measure(0, 0);
        return view.getWidth();
    }
    
    /**
     * 设置高度
     * @param view	控件
     * @param height	高度	
     */
    public static void setHeight(View view, int height) {
    	if (view == null || view.getLayoutParams() == null) {
    		throw new IllegalArgumentException("View LayoutParams is null");
		}
    	ViewGroup.LayoutParams params = view.getLayoutParams();
    	params.height = height;
    	view.setLayoutParams(params);
    }
    
    /**
     * 设置View的宽度
     * @param view	view
     * @param width	宽度
     */
    public static void setWidth(View view, int width){
        if(view == null || view.getLayoutParams() == null){
            throw new IllegalArgumentException("View LayoutParams is null");
        }
 
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        view.setLayoutParams(params);
    }
    
    /**
     * 设置ListView的实际高度
     * @param listView	ListView控件
     */
    public static void setListHeight(ListView listView) {
    	if (listView == null) {
    		throw new IllegalArgumentException("ListView is null");
		}
    	Adapter adapter = listView.getAdapter();
    	if (adapter == null) {
			return;
		}
    	int totalHeight = 0;
    	int size = adapter.getCount();
    	for (int i = 0; i < size; i++) {
			View listItem = adapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
    	ViewGroup.LayoutParams params = listView.getLayoutParams();
    	params.height = totalHeight + (listView.getDividerHeight() * (size - 1));
    	LogUtil.d("MeasureUtil", "listview-height--" + params.height);
        listView.setLayoutParams(params);
    }
    
    /**
     * 设置GridView的高度,
     * @param context	应用程序上下文
     * @param gv		GridView控件
     * @param n			行数
     * @param m			列数
     */
    public static void setGridViewHeight(Context context, GridView gv, int n, int m) {
    	if(gv == null){
    		throw new IllegalArgumentException("GridView is null");
    	}
    	Adapter adapter = gv.getAdapter();
    	if (adapter == null) {
			return;
		}
    	int totalHeight = 0;
    	int size = adapter.getCount();
    	for (int i = 0; i < size; i++) {
			View listItem = adapter.getView(i, null, gv);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight() + ScreenUtil.dp2px(context, m);
		}
    	ViewGroup.LayoutParams params = gv.getLayoutParams();
    	params.height = totalHeight + gv.getPaddingTop() + gv.getPaddingBottom() + 2;
    	LogUtil.d("MeasureUtil", "gridview-height--" + params.height);
    	gv.setLayoutParams(params);
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值