下面列出了android.support.v7.widget.GridLayout#LayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setTraits(Asset asset) {
if (asset.getTraits() != null && !asset.getTraits().isEmpty()) {
if (asset.getAssetContract().getName().equals("CryptoKitties")) {
labelAttributes.setText(R.string.label_cattributes);
} else {
labelAttributes.setText(R.string.label_attributes);
}
for (Trait trait : asset.getTraits()) {
View attributeView = View.inflate(this, R.layout.item_attribute, null);
TextView traitType = attributeView.findViewById(R.id.trait);
TextView traitValue = attributeView.findViewById(R.id.value);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, 1f),
GridLayout.spec(GridLayout.UNDEFINED, 1f));
attributeView.setLayoutParams(params);
traitType.setText(trait.getTraitType());
traitValue.setText(trait.getValue());
grid.addView(attributeView);
}
} else {
labelAttributes.setVisibility(View.GONE);
}
}
/**
* 根据屏幕方向创建按钮
*/
public void createActionButton() {
if (!actionBtnArray.isEmpty()) actionBtnArray.clear();
for (int rowIndex = 0; rowIndex < currentPageRow; rowIndex++)
for (int colIndex = 0; colIndex < currentPageCol; colIndex++) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_action_button, null);
ViewGroup actionBtn = view.findViewById(R.id.actionBtn);
actionBtn.setTag(getButtonIndex(rowIndex, colIndex));
actionBtn.setOnClickListener(this);
GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(
GridLayout.spec(rowIndex, 1f),
GridLayout.spec(colIndex, 1f)
);
gridLayoutParam.setMargins(1, 1, 1, 1);
gridLayoutParam.height = 0;
gridLayoutParam.width = 0;
addView(view, gridLayoutParam);
UiButtonItem item = new UiButtonItem();
item.button = actionBtn;
item.imageView = view.findViewById(R.id.actionBtnBg);
item.textView = view.findViewById(R.id.actionBtnText);
actionBtnArray.put(getButtonIndex(rowIndex, colIndex), item);
}
}
/**
* 将倒影布局添加到GridLayout容器中
*/
private void addReflectionImageView() {
mReflectionImageView = new ImageView(mContext);
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
layoutParams.width = mColumnCount * mBaseWidth
+ ((mColumnCount > 1 ? mHorizontalMargin * (mColumnCount - 1) : 0));
layoutParams.height = mBaseHeight;
layoutParams.rowSpec = GridLayout.spec(mRowCount - 1, 1);
layoutParams.columnSpec = GridLayout.spec(0, mColumnCount);
mReflectionImageView.setLayoutParams(layoutParams);
mReflectionImageView.setScaleType(ImageView.ScaleType.FIT_XY);
this.refreshReflection(0);
mGridLayout.addView(mReflectionImageView);
mReflectionImageView.setVisibility(mVisibility);
}
private View createOptionIcon(GridLayout parent, int rowIndex, boolean editIconExists) {
// The icon has a pre-defined width.
ImageView optionIcon = new ImageView(parent.getContext());
optionIcon.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
if (mOption.isEditable()) {
optionIcon.setMaxWidth(mEditableOptionIconMaxWidth);
} else {
optionIcon.setMaxWidth(mNonEditableOptionIconMaxWidth);
}
optionIcon.setAdjustViewBounds(true);
optionIcon.setImageDrawable(mOption.getDrawableIcon());
// Place option icon at column three if no edit icon.
int columnStart = editIconExists ? 2 : 3;
GridLayout.LayoutParams iconParams =
new GridLayout.LayoutParams(GridLayout.spec(rowIndex, 1, GridLayout.CENTER),
GridLayout.spec(columnStart, 1, GridLayout.CENTER));
iconParams.topMargin = mVerticalMargin;
parent.addView(optionIcon, iconParams);
optionIcon.setOnClickListener(OptionSection.this);
return optionIcon;
}
private GridLayout.LayoutParams getLayoutParams() {
GridLayout.LayoutParams layoutParams;
layoutParams = new GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, 1f),
GridLayout.spec(GridLayout.UNDEFINED, 1f));
layoutParams.width = 0;
return layoutParams;
}
private View getColorItemView(final Context context, int position, boolean isSelect) {
int color = mColors[position];
int widthImageCheckView = Utils.convertDensityPix(context, 24);
int widthColorView = Utils.convertDensityPix(context, 56);
int widthMargin = Utils.convertDensityPix(context, 4);
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.ic_done_white_24dp);
FrameLayout.LayoutParams ivParams = new FrameLayout.LayoutParams(widthImageCheckView, widthImageCheckView);
ivParams.gravity = Gravity.CENTER;
imageView.setLayoutParams(ivParams);
imageView.setVisibility(isSelect ? View.VISIBLE : View.INVISIBLE);
FrameLayout frameLayout = new FrameLayout(context);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(new FrameLayout.LayoutParams(widthColorView, widthColorView));
params.setGravity(Gravity.CENTER);
params.setMargins(widthMargin, widthMargin, widthMargin, widthMargin);
frameLayout.setLayoutParams(params);
setBackgroundSelector(frameLayout, color);
frameLayout.addView(imageView);
frameLayout.setOnClickListener(this);
frameLayout.setTag(position);
return frameLayout;
}
private View createEditIcon(GridLayout parent, int rowIndex) {
View editorIcon = LayoutInflater.from(parent.getContext())
.inflate(R.layout.payment_option_edit_icon, null);
// The icon floats to the right of everything.
GridLayout.LayoutParams iconParams =
new GridLayout.LayoutParams(GridLayout.spec(rowIndex, 1, GridLayout.CENTER),
GridLayout.spec(3, 1, GridLayout.CENTER));
iconParams.topMargin = mVerticalMargin;
parent.addView(editorIcon, iconParams);
editorIcon.setOnClickListener(OptionSection.this);
return editorIcon;
}
/**
* 向GridLayout容器中添加Grid元素
*
* @param gridItem Grid元素
*/
private GridBuilder addItem(GridItem gridItem) {
View itemLayout = null;
if (null != mOnViewCreateCallBack) {
itemLayout = mOnViewCreateCallBack.onViewCreate(mLayoutInflater, null == mGridViewHolder
? null : mGridViewHolder.getConvertView(), gridItem);
}
if (null == itemLayout) {
return this;
}
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
// 优先根据预先取得的width/height设置,但不影响跨列/行数
layoutParams.width = (gridItem.getWidth() > 0 ? gridItem.getWidth() : gridItem.getColumnSpec() * mBaseWidth)
+ ((gridItem.getColumnSpec() > 1 && gridItem.getWidth() <= 0 ? mHorizontalMargin * (gridItem.getColumnSpec() - 1) : 0));
layoutParams.height = (gridItem.getHeight() > 0 ? gridItem.getHeight() : gridItem.getRowSpec() * mBaseHeight)
+ ((gridItem.getRowSpec() > 1 && gridItem.getWidth() <= 0 ? mVerticalMargin * (gridItem.getRowSpec() - 1) : 0));
if (gridItem.getWidth() <= 0) {
gridItem.setWidth(layoutParams.width);
}
if (gridItem.getHeight() <= 0) {
gridItem.setHeight(layoutParams.height);
}
layoutParams.rowSpec = GridLayout.spec(gridItem.getRow(), gridItem.getRowSpec());
layoutParams.columnSpec = GridLayout.spec(gridItem.getColumn(), gridItem.getColumnSpec());
// 设置每个item间距,最外层间距也需要设置(因为需要体现边缘item的scale效果)
if (gridItem.getRow() > 0) {
layoutParams.topMargin = mVerticalMargin;
}
if (gridItem.getColumn() > 0) {
layoutParams.leftMargin = mHorizontalMargin;
}
itemLayout.setLayoutParams(layoutParams);
itemLayout.setFocusable(true);
itemLayout.setClickable(true);
itemLayout.setOnFocusChangeListener(this);
itemLayout.setOnClickListener(this);
itemLayout.setOnKeyListener(mOnKeyListener);
itemLayout.setSoundEffectsEnabled(false);
if (mGridLayout.getChildCount() == 0 && gridItem == mGridItemList.get(0)) {
itemLayout.setTag(GridItem.TAG_FIRST_ITEM);
}
this.mGridLayout.addView(itemLayout);
return this;
}
Exchanger(final Context context, final GaService service, final View mView, final boolean isBuyPage, final OnCalculateCommissionFinishListener listener) {
mContext = context;
mService = service;
mIsBuyPage = isBuyPage;
mOnCalculateCommissionFinishListener = listener;
mAmountFiatWithCommission = UI.find(mView, R.id.amountFiatWithCommission);
mAmountBtcWithCommission = UI.find(mView, R.id.amountBtcWithCommission);
final FontAwesomeTextView bitcoinUnitText = UI.find(mView, R.id.sendBitcoinUnitText2);
UI.setCoinText(mService, bitcoinUnitText, null, null);
final String currency = mService.getFiatCurrency();
final FontAwesomeTextView fiatView = UI.find(mView, R.id.commissionFiatIcon);
AmountFields.changeFiatIcon(fiatView, currency);
if (mService.isElements()) {
bitcoinUnitText.setText(String.format("%s ", mService.getAssetSymbol()));
UI.hide(UI.find(mView, R.id.commissionFiatColumn));
}
mAmountFiatEdit = UI.find(mView, R.id.sendAmountFiatEditText);
mAmountBtcEdit = UI.find(mView, R.id.sendAmountEditText);
final String btnsValue = service.cfg().getString("exchanger_fiat_btns", "");
if (!btnsValue.isEmpty()) {
final String[] btnsValueArray = btnsValue.split(" ");
final GridLayout gridLayout = UI.find(mView, R.id.gridLayout);
for (final String value : btnsValueArray) {
final Button btn = new Button(mContext);
btn.setText(String.format("%s %s", value, currency));
final GridLayout.Spec spec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
final GridLayout.LayoutParams param = new GridLayout.LayoutParams(spec, spec);
btn.setLayoutParams(param);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
mAmountFiatEdit.setText(value);
if (mService.isElements())
mAmountBtcEdit.setText(value);
}
});
gridLayout.addView(btn);
}
}
}
/**
* Updates the total and how it's broken down.
*
* @param cart The shopping cart contents and the total.
*/
public void update(ShoppingCart cart) {
Context context = mBreakdownLayout.getContext();
CharSequence totalPrice = createValueString(
cart.getTotal().getCurrency(), cart.getTotal().getPrice(), true);
// Show the updated text view if the total changed.
showUpdateIfTextChanged(totalPrice);
// Update the summary to display information about the total.
setSummaryText(cart.getTotal().getLabel(), totalPrice);
mBreakdownLayout.removeAllViews();
if (cart.getContents() == null) return;
int maximumDescriptionWidthPx =
((View) mBreakdownLayout.getParent()).getWidth() * 2 / 3;
// Update the breakdown, using one row per {@link LineItem}.
int numItems = cart.getContents().size();
mBreakdownLayout.setRowCount(numItems);
for (int i = 0; i < numItems; i++) {
LineItem item = cart.getContents().get(i);
TextView description = new TextView(context);
ApiCompatibilityUtils.setTextAppearance(description, item.getIsPending()
? R.style.PaymentsUiSectionPendingTextEndAligned
: R.style.PaymentsUiSectionDescriptiveTextEndAligned);
description.setText(item.getLabel());
description.setEllipsize(TruncateAt.END);
description.setMaxLines(2);
if (maximumDescriptionWidthPx > 0) {
description.setMaxWidth(maximumDescriptionWidthPx);
}
TextView amount = new TextView(context);
ApiCompatibilityUtils.setTextAppearance(amount, item.getIsPending()
? R.style.PaymentsUiSectionPendingTextEndAligned
: R.style.PaymentsUiSectionDescriptiveTextEndAligned);
amount.setText(createValueString(item.getCurrency(), item.getPrice(), false));
// Each item is represented by a row in the GridLayout.
GridLayout.LayoutParams descriptionParams = new GridLayout.LayoutParams(
GridLayout.spec(i, 1, GridLayout.END),
GridLayout.spec(0, 1, GridLayout.END));
GridLayout.LayoutParams amountParams = new GridLayout.LayoutParams(
GridLayout.spec(i, 1, GridLayout.END),
GridLayout.spec(1, 1, GridLayout.END));
ApiCompatibilityUtils.setMarginStart(amountParams,
context.getResources().getDimensionPixelSize(
R.dimen.payments_section_descriptive_item_spacing));
mBreakdownLayout.addView(description, descriptionParams);
mBreakdownLayout.addView(amount, amountParams);
}
}
private TextView createLabel(GridLayout parent, int rowIndex, boolean optionIconExists,
boolean editIconExists, boolean isEnabled) {
Context context = parent.getContext();
Resources resources = context.getResources();
// By default, the label appears to the right of the "button" in the second column.
// + If there is no button, no option and edit icon, the label spans the whole row.
// + If there is no option and edit icon, the label spans three columns.
// + If there is no edit icon or option icon, the label spans two columns.
// + Otherwise, the label occupies only its own column.
int columnStart = 1;
int columnSpan = 1;
if (!optionIconExists) columnSpan++;
if (!editIconExists) columnSpan++;
TextView labelView = new TextView(context);
if (mRowType == OPTION_ROW_TYPE_OPTION) {
// Show the string representing the PaymentOption.
ApiCompatibilityUtils.setTextAppearance(labelView, isEnabled
? R.style.PaymentsUiSectionDefaultText
: R.style.PaymentsUiSectionDisabledText);
labelView.setText(convertOptionToString(mOption,
mDelegate.isBoldLabelNeeded(OptionSection.this),
false /* singleLine */));
labelView.setEnabled(isEnabled);
} else if (mRowType == OPTION_ROW_TYPE_ADD) {
// Shows string saying that the user can add a new option, e.g. credit card no.
String typeface = resources.getString(R.string.roboto_medium_typeface);
int textStyle = resources.getInteger(R.integer.roboto_medium_textstyle);
int buttonHeight = resources.getDimensionPixelSize(
R.dimen.payments_section_add_button_height);
ApiCompatibilityUtils.setTextAppearance(
labelView, R.style.PaymentsUiSectionAddButtonLabel);
labelView.setMinimumHeight(buttonHeight);
labelView.setGravity(Gravity.CENTER_VERTICAL);
labelView.setTypeface(Typeface.create(typeface, textStyle));
} else if (mRowType == OPTION_ROW_TYPE_DESCRIPTION) {
// The description spans all the columns.
columnStart = 0;
columnSpan = 4;
ApiCompatibilityUtils.setTextAppearance(
labelView, R.style.PaymentsUiSectionDescriptiveText);
} else if (mRowType == OPTION_ROW_TYPE_WARNING) {
// Warnings use three columns.
columnSpan = 3;
ApiCompatibilityUtils.setTextAppearance(
labelView, R.style.PaymentsUiSectionWarningText);
}
// The label spans two columns if no option or edit icon, or spans three columns if
// no option and edit icons. Setting the view width to 0 forces it to stretch.
GridLayout.LayoutParams labelParams =
new GridLayout.LayoutParams(GridLayout.spec(rowIndex, 1, GridLayout.CENTER),
GridLayout.spec(columnStart, columnSpan, GridLayout.FILL, 1f));
labelParams.topMargin = mVerticalMargin;
labelParams.width = 0;
if (optionIconExists) {
// Margin at the end of the label instead of the start of the option icon to
// allow option icon in the the next row align with the end of label (include
// end margin) when edit icon exits in that row, like below:
// ---Label---------------------[label margin]|---option icon---|
// ---Label---[label margin]|---option icon---|----edit icon----|
ApiCompatibilityUtils.setMarginEnd(labelParams, mLargeSpacing);
}
parent.addView(labelView, labelParams);
labelView.setOnClickListener(OptionSection.this);
return labelView;
}