About grid layout gridLayout, dynamically create content

Important attributes:

android:rowCount (number of rows)
android:columnCount (number of columns)
android:layout_row (in which row)
android:layout_rowSpan (across several rows)
insert image description here

<?xml version="1.0" encoding="utf-8"?>

  <EditText
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnSpan="4"
        android:layout_columnWeight="1"
        android:layout_rowWeight="2"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="C"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="+/-"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="%"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="*"/>
 
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="0"
        android:layout_columnSpan="2"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="."/>
    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:text="="/>

</GridLayout>

You can also dynamically create the buttons inside

First create a gridLayout in the xml file, and set android:columnCount="4" how many to display in each row;

 <GridLayout
                    android:id="@+id/grid_result_string"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/grey_btn_background"
                    android:columnCount="10"
                    >

Then according to the requirements, after creating the view, add it to the GridLayout through addView

for(int i = 0;i < barcode.length() ;i ++)
      {
    
    
         String substring = barcode.substring(i, i + 1);

         TextView textView = new TextView(this);
         textView.setTextSize(21);
         textView.setText(substring);

         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(55,55);
         layoutParams.setMargins(3,3,3,3);
         textView.setPadding(1,1,1,1);
         textView.setLayoutParams(layoutParams);
         textView.setGravity(Gravity.CENTER);

         textView.setBackgroundColor(getResources().getColor(R.color.white));
         grid_result_string.addView(textView);
      }

What I am doing here are ten ordinary frames, which are dynamically generated;
insert image description here

Regarding the new control, there is no way to directly set the margin problem

It needs to be set in linearLayout.LayoutParams, such as the above code;

1. If the control is defined in XML

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textview.getLayoutParams();
lp.leftMargin = 0;
textview.setLayoutParams(lp);

2. If this control is new from us, we will find that there will be a null pointer error when using the above method. Then we use another method

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);//4个参数按顺序分别是左上右下
textview.setLayoutParams(layoutParams);

Original link: https://codeleading.com/article/27194067916/

————————————————
Copyright statement: This article is an original article of CSDN blogger "SlowIsFastLemon", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting .
Original link: https://blog.csdn.net/SlowIsFastLemon/article/details/118059337

Guess you like

Origin blog.csdn.net/huangerbian/article/details/130749954