gpt4 book ai didi

android - 如何设置android :layout_columnWeight ="1" programmatically to an element in an android support v7 Gridlayout

转载 作者:IT老高 更新时间:2023-10-28 21:55:59 28 4
gpt4 key购买 nike

我正在尝试以编程方式构建一个包含 2 列的 GirdLayout,并且我希望这些列的宽度相等,设置为屏幕宽度的一半。

我发现您可以从 API 21 或支持 v7 GirdLayout View 开始执行此操作。我看到使用 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);

最佳答案

我从 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);

关于android - 如何设置android :layout_columnWeight ="1" programmatically to an element in an android support v7 Gridlayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655352/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com