码迷,mamicode.com
首页 > 移动开发 > 详细

android-gridView item 大小调节

时间:2015-01-22 00:01:40      阅读:468      评论:0      收藏:0      [点我收藏+]

标签:

 public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    } 

 @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View childView = getChildAt(i);
            if (childView.getVisibility() != View.GONE) {
                final int childWidth = childView.getMeasuredWidth();
/* Vanzo:zhangshuli on: Wed, 21 Jan 2015 18:12:28 +0000
                childView.layout(childLeft, 0, childLeft + childWidth,
                        childView.getMeasuredHeight());
 */
                int top = (int) mContext.getResources().getDimension(R.dimen.share_item_port_top);
                childView.layout(childLeft, top, childLeft + childWidth,
                        childView.getMeasuredHeight());
// End of Vanzo: zhangshuli
                childLeft += childWidth;
            }
        }
    }
 @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                if (MmsConfig.getIpMessagServiceId(mContext) == IpMessageServiceId.NO_SERVICE) {
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.share_grid_common_item, null);
                } else {
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.share_grid_item, null);
                }   
                convertView.setTag(convertView);
            } else {
                convertView = (View) convertView.getTag();
            }   
/* Vanzo:zhangshuli on: Wed, 21 Jan 2015 18:16:59 +0000
 */
            int height = (int) mContext.getResources().getDimension(R.dimen.share_item_port_height);
            int width = (int) mContext.getResources().getDimension(R.dimen.share_item_port_width);
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(width, height);
            convertView.setLayoutParams(param);
// End of Vanzo:zhangshuli

            TextView text = (TextView) convertView.findViewById(R.id.tv_share_name);
            ImageView img = (ImageView) convertView.findViewById(R.id.iv_share_icon);
            if (position < mStringArray.length) {
                text.setText(mStringArray[position]);
                img.setImageResource(mIconArray[position]);
            }   
            return convertView;
        }   
    }   

 

android-gridView item 大小调节

标签:

原文地址:http://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_zj_15012022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!