private void addTableRow(String key, String val) { TableLayout tl = (TableLayout) findViewById(R.id.data_table); TableRow tr = new TableRow(this); MarginLayoutParams params = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN); tr.setLayoutParams(params); tr.setBackgroundColor(Color.BLACK); TextView name = new TextView(this); name.setGravity(Gravity.RIGHT); name.setText(key + ": "); TextView value = new TextView(this); value.setGravity(Gravity.LEFT); value.setText(val); tr.addView(name); tr.addView(value); tl.addView( tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); /* * TODO remove this hack * * let's define a limit number of rows */ if (tl.getChildCount() > 10) tl.removeViewAt(0); }
/** * 重新计算view的Margin * * @param view * @return */ public static void remargin(View view, float horizontalRatio, float verticalRatio) { MarginLayoutParams marginParams = null; try { marginParams = (MarginLayoutParams) view.getLayoutParams(); } catch (ClassCastException e) { return; } if (marginParams == null) return; int left = (int) (marginParams.leftMargin * horizontalRatio); int top = (int) (marginParams.topMargin * verticalRatio); int right = (int) (marginParams.rightMargin * horizontalRatio); int bottom = (int) (marginParams.bottomMargin * verticalRatio); marginParams.setMargins(left, top, right, bottom); view.setLayoutParams(marginParams); }
/* * Sets contact photo to a default placeholder image */ private void setContactPhotoToDefault(ImageView photoImageView) { // Reset background and padding photoImageView.setBackgroundResource(0); photoImageView.setPadding(0, 0, 0, 0); // Set margins for placeholder image MarginLayoutParams mLP = (MarginLayoutParams) photoImageView.getLayoutParams(); final int scaledMargin = (int) (contactPhotoDefaultMargin * this.getResources().getDisplayMetrics().density); mLP.setMargins(scaledMargin, scaledMargin, scaledMargin, scaledMargin); photoImageView.setLayoutParams(mLP); // Set placeholder image photoImageView.setImageDrawable(contactPhotoPlaceholderDrawable); }
private void addTableRow(String id, String key, String val) { TableRow tr = new TableRow(this); MarginLayoutParams params = new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN, TABLE_ROW_MARGIN); tr.setLayoutParams(params); TextView name = new TextView(this); name.setGravity(Gravity.RIGHT); name.setText(key + ": "); TextView value = new TextView(this); value.setGravity(Gravity.LEFT); value.setText(val); value.setTag(id); tr.addView(name); tr.addView(value); tl.addView(tr, params); }
public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; View listItem = null; for (int i = 0; i < listAdapter.getCount(); i++) { listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); listView.setLayoutParams(params); }
// 删除状态图片 private void deleteNewsImageView(String tag) { // 删除 int subviewsCount = addImageLayout.getChildCount(); for (int i = 0; i < subviewsCount; i++) { View view = addImageLayout.getChildAt(i); if (null != view.getTag() && view.getTag().equals(tag)) { addImageLayout.removeViewAt(i); break; } } // 添加按钮位置重置 MarginLayoutParams addlp = (MarginLayoutParams) addImageView.getLayoutParams(); addlp.setMargins(oriMarginLeft, 0, 0, 0); // 删除之后重新排序 subviewsCount = addImageLayout.getChildCount(); for (int i = 1; i < subviewsCount; i++) { View view = addImageLayout.getChildAt(i); moveImageView((ImageView) view, i); } }
// 移动位置 private void moveImageView(ImageView imageView, int imageCount) { int columnNum = imageCount % 4; int lineNum = imageCount / 4; // 添加按钮位置 MarginLayoutParams addlp = (MarginLayoutParams) addImageView.getLayoutParams(); // LogUtils.i(columnNum+" "+lineNum+" "+addlp.leftMargin+" "+addImageView.getWidth()+" // "+addlp.width, // 1); int width = addlp.width; int height = addlp.height; // 布局位置 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height); lp.setMargins(addlp.leftMargin, addlp.topMargin, 0, 0); imageView.setLayoutParams(lp); addlp.setMargins(oriMarginLeft + (width + space) * columnNum, (height + 10) * lineNum, 0, 0); if (imageCount > 8) { addImageView.setVisibility(View.GONE); } else { addImageView.setVisibility(View.VISIBLE); } }
/* * Sets contact photo to the target imageview */ private void setContactPhoto(ImageView photoImageView, Bitmap contactPhoto) { if (contactPhoto == null) { setContactPhotoToDefault(photoImageView); return; } // Update background and padding if (SmsPopupUtils.PRE_ECLAIR) { photoImageView.setBackgroundResource(android.R.drawable.picture_frame); } else { photoImageView.setBackgroundResource(R.drawable.quickcontact_badge_small); } // Set margins for image MarginLayoutParams mLP = (MarginLayoutParams) photoImageView.getLayoutParams(); final int scaledMargin = (int) (contactPhotoMargin * this.getResources().getDisplayMetrics().density); mLP.setMargins(scaledMargin, scaledMargin, scaledMargin, scaledMargin); photoImageView.setLayoutParams(mLP); // Set contact photo image photoImageView.setImageBitmap(contactPhoto); }