[英]How do I set android:layout_columnWeight="1" programmatically to an element in an android support v7 Gridlayout
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.我正在尝试以编程方式构建一个包含 2 列的 GirdLayout,我希望这些列的宽度设置为屏幕宽度的一半。
I figured out you can do this since API 21 or with the support v7 GirdLayout view.我发现您可以从 API 21 开始或在支持 v7 GirdLayout 视图的情况下执行此操作。 I see examples that uses android:layout_columnWeight="1" to do this.
我看到使用 android:layout_columnWeight="1" 来执行此操作的示例。 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);
}
}
}
SOLUTION:解决方案:
GridLayout.LayoutParams parem = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f), GridLayout.spec(GridLayout.UNDEFINED, 1f));
v.setLayoutParams(parem);
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 我在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);
-Here you are ! -这个给你 ! :>
:>
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));
param.height = 0;
param.width = 0;
button.setLayoutParams(param);
This functions of GridLayout.Spec will allow you to set the weight correctly: 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)
usage example: 用法示例:
((LayoutParams) gm.getViewFromGridCoords(i, j).getLayoutParams()).columnSpec = GridLayout.spec(j, span, CENTER, 1);
For anyone using xamarin.android, these answers helped me, but I had to change the syntax a bit. 对于任何使用xamarin.android的人来说,这些答案对我有帮助,但我不得不改变语法。 So if anyone else is using xamarin.android here is the syntax:
所以,如果其他人正在使用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;
Where view
is the view contained in the GridLayout. 其中
view
是GridLayout中包含的视图。
I have faced same problem and tried lots of things.我遇到了同样的问题并尝试了很多东西。 My required design is as followings.
我需要的设计如下。
Finally, I solved this by following approach.最后,我通过以下方法解决了这个问题。
I add a grid layout to my xml file as follows我将网格布局添加到我的 xml 文件中,如下所示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridLayout
android:id="@+id/gridLayoutBillingInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
</GridLayout>
</RelativeLayout>
Then I created a custom layout for each item in the grid layout.然后我为网格布局中的每个项目创建了一个自定义布局。 Only adding textview doesn't work but when put textview inside layout then it works.
仅添加 textview 不起作用,但是当将 textview 放入布局中时,它就起作用了。 See below -
见下文 -
public class ItemLayout : LinearLayout {
constructor(context: Context, text:String) :super(context){
this.orientation = HORIZONTAL
this.gravity = Gravity.CENTER_VERTICAL
val param = GridLayout.LayoutParams(
GridLayout.spec(
GridLayout.UNDEFINED, GridLayout.FILL, 1f
),
GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f)
)
param.height = ViewGroup.LayoutParams.WRAP_CONTENT
param.width = 0
this.layoutParams = param
var textView = TextView(context)
textView.setText(text)
this.addView(textView)
}
}
finally add this item to grid layout with data.最后将此项添加到带有数据的网格布局。 See below -
见下文 -
fun addDataToGridLayout{
val gridLayoutBillingInfo = findViewById<GridLayout>(R.id.gridLayoutBillingInfo)
val billDetailList : ArrayList<String> = ArrayList()
billDetailList.add("test data 1")
billDetailList.add("test data 2")
billDetailList.add("test data 3")
billDetailList.add("test data 4")
billDetailList.add("test data 5")
billDetailList.add("test data 6")
billDetailList.forEach {
gridLayoutBillingInfo.addView(BillItemLayout(context,it))
}
} }
I hope it will help.我希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.