如何以编程方式将android:layout_columnWeight =“1”设置为android support v7 Gridlayout中的某个元素?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (275)

我想你可以做到这一点,因为API 21或支持v7 GirdLayout视图。我看到使用android:layout_columnWeight =“1”来做到这一点的例子。但我无法找到如何以编程方式执行此操作。

任何人都可以帮我解决这个问题吗?

package com.tunify.v3.component;

import java.util.ArrayList;

import android.support.v7.widget.GridLayout;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;

import com.tunify.asme.data.ChangeableSelectionParameterValue;

public class ChangeableSelectionParameters extends GridLayout{

//private ArrayList<ChangeableSelectionParameterView> selectionParameterViews;
private ArrayList<ChangeableSelectionParameterValue> selectionParameterValues;

private static final int ITEM_PADDING_LEFT_DP = 0;
private static final int ITEM_PADDING_RIGHT_DP = 0;
private static final int ITEM_PADDING_TOP_DP = 0;
private static final int ITEM_PADDING_BOTTOM_DP = 0;

//private static final int MUSICCOLLECTION_DISPLAY_TEXT_SIZE_SP = 15;


public ChangeableSelectionParameters(android.content.Context context, ArrayList<ChangeableSelectionParameterValue> selectionParameterValues) {
    super(context);

    this.selectionParameterValues = selectionParameterValues;

    initializeLayoutBasics(context);
    initializeComponents();
}

private void initializeLayoutBasics(android.content.Context context) {
    setOrientation(VERTICAL);
    setColumnCount(2);
    setRowCount((int)Math.ceil(selectionParameterValues.size()/2.0));

    int width = ViewGroup.LayoutParams.MATCH_PARENT;
    int height = ViewGroup.LayoutParams.WRAP_CONTENT;
    android.view.ViewGroup.LayoutParams layoutParams = new android.view.ViewGroup.LayoutParams(width, height);
    this.setLayoutParams(layoutParams);

    DisplayMetrics metrics = getResources().getDisplayMetrics();

    int paddingLeftPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_LEFT_DP, metrics);
    int paddingRightPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_RIGHT_DP, metrics);
    int paddingTopPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_TOP_DP, metrics);
    int paddingBottomPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_BOTTOM_DP, metrics);
    setPadding(paddingLeftPx, paddingTopPx, paddingRightPx, paddingBottomPx);

}

private void initializeComponents() {

    for(ChangeableSelectionParameterValue param : this.selectionParameterValues){
        ChangeableSelectionParameterView v = new ChangeableSelectionParameterView(getContext(), param);
        //TODO set views layout column weight to 1
        this.addView(v);
    }
 }
}

解:

 GridLayout.LayoutParams parem = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f),      GridLayout.spec(GridLayout.UNDEFINED, 1f));
 v.setLayoutParams(parem);
提问于
用户回答回答于
用户回答回答于

使用这样的方法试试:

Button button = new Button(this);
GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
            GridLayout.UNDEFINED,GridLayout.FILL,1f),
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
button.setLayoutParams(param);

扫码关注云+社区