如何设置android:layout_columnWeight =“" 1"以编程方式设置为Android支持v7 Gridlayout中的元素 [英] How do I set android:layout_columnWeight="1" programmatically to an element in an android support v7 Gridlayout

查看:208
本文介绍了如何设置android:layout_columnWeight =“" 1"以编程方式设置为Android支持v7 Gridlayout中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build a GirdLayout programmatically with 2 columns and I want these columns to have equal width set at half the width of the screen.

我发现您可以从API 21或支持v7 GirdLayout视图开始执行此操作.我看到了使用android:layout_columnWeight ="1"来执行此操作的示例.但是我找不到如何以编程方式执行此操作.

I figured out you can do this since API 21 or with the support v7 GirdLayout view. I see examples that uses android:layout_columnWeight="1" to do this. But I can not find how to do this programmatically.

有人可以帮我解决这个问题吗?

Can anyone help me with this matter?

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中找到了此方法:

I found this method from 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

所以也许您可以尝试这样的事情:

So maybe you could try something like this:

((GridLayout.LayoutParams) this.getLayoutParams()).columnSpec =
    GridLayout.spec(GridLayout.UNDEFINED, 1f);

这篇关于如何设置android:layout_columnWeight =“&quot; 1&quot;以编程方式设置为Android支持v7 Gridlayout中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆