[英]Set columnWeight on EditText
I'm using a GridLayout in my activity and I used a couple EditText to fill the grid. 我在活动中使用了GridLayout,并使用了几个EditText来填充网格。 The width and everything looked good.
宽度和一切看起来不错。 Now I want to add the EditTexts dynamic to the existing GridLayout.
现在,我想将EditTexts动态添加到现有的GridLayout中。 But I have some problems to translate the XML attributes to Java.
但是我在将XML属性转换为Java时遇到一些问题。 Especially I don't know how to set the columnWeight correct.
特别是我不知道如何设置columnWeight正确。
This is the GridLayout: 这是GridLayout:
<GridLayout
android:id="@+id/cellGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="8"
android:rowCount="5">
...
</GridLayout>
This was one of the correct looking XML EditTexts: 这是看起来正确的XML EditTexts之一:
<EditText
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:background="@color/middelGrey"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textSize="12dp" />
And this is how I try to add the EditTexts to the Grid in a for-loop 这就是我尝试在for循环中将EditTexts添加到网格的方式
for(int i=0; i < amountOfCells/2; i++){
EditText editText = new EditText(getContext());
editText.setBackgroundColor(getResources().getColor(R.color.middelGrey, null));
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
llp.setMargins(0, 0, 2, 2);
editText.setLayoutParams(llp);
editText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
editText.setWidth(20);
editText.setHeight(wrap_content);
gridLayout.addView(editText, i);
}
But it doesn't look like the xml version, I think the columnWeight is not set correct. 但是它看起来不像xml版本,我认为columnWeight设置不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.