android-gridView item 大小调节

 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;
        }   
    }   

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。