我正在尝试使用2列以编程方式构建GirdLayout,并且我希望这些列的宽度设置为屏幕宽度的一半。
我发现你可以在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);
答案 0 :(得分:26)
我在GridLayout中找到了这个方法:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/android/widget/GridLayout.java#GridLayout.spec%28int%2Cfloat%29
所以也许你可以尝试这样的事情:
((GridLayout.LayoutParams) this.getLayoutParams()).columnSpec =
GridLayout.spec(GridLayout.UNDEFINED, 1f);
答案 1 :(得分:1)
GridLayout.Spec的这个功能将允许您正确设置权重:
public static Spec spec(int start, int size, Alignment alignment, float weight)
public static Spec spec(int start, Alignment alignment, float weight)
public static Spec spec(int start, int size, float weight)
public static Spec spec(int start, float weight)
用法示例:
((LayoutParams) gm.getViewFromGridCoords(i, j).getLayoutParams()).columnSpec = GridLayout.spec(j, span, CENTER, 1);
答案 2 :(得分:1)
对于任何使用xamarin.android的人来说,这些答案对我有帮助,但我不得不稍微改变语法。因此,如果其他人使用xamarin.android,则语法为:
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.RowSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
param.ColumnSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
view.LayoutParameters = param;
其中view
是GridLayout中包含的视图。