代码之家  ›  专栏  ›  技术社区  ›  miosz

在android constraintlayout中创建网格(n×n),变量数为n

  •  2
  • miosz  · 技术社区  · 8 年前

    我想在 ConstraintLayout 内创建一个正方形网格。我的第一个想法是创建一个水平链,给出一些边界值,并将其设置为所有单个视图大小属性 width=match_constraint->code>, height=match_constraint->code>并将比率设置为1:1。它起作用,看起来像:

    当网格大小为2_2时很容易-有4个元素,所以很容易。但是当我必须创建一个网格7_7时,我应该怎么做?我们有49个视图,所以设置所有这些视图可能很棘手。我想在约束布局中这样做,因为我想要一个灵活的布局。))所有单一视图大小属性 width = match_constraint , height = match_constraint 并将比率设置为1:1。它起作用,看起来像:

    enter image description here

    当网格大小为2_2时很容易-有4个元素,所以很容易。但是当我必须创建一个网格7_7时,我应该怎么做?我们有49个视图,所以设置所有这些视图可能很棘手。我想在约束布局中这样做,因为我想要一个灵活的布局。:)

    2 回复  |  直到 7 年前
        1
  •  2
  •   Cheticamp    8 年前

    ConstraintLayout match_constraints View Space

    enter image description here

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <View
            android:id="@+id/gridFrame"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_margin="16dp"
            android:background="@android:color/holo_blue_light"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    enter image description here

    public class MainActivity extends AppCompatActivity {
        int mRows = 7;
        int mCols = 7;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ConstraintLayout layout = findViewById(R.id.layout);
    
            int color1 = getResources().getColor(android.R.color.holo_red_light);
            int color2 = getResources().getColor(android.R.color.holo_blue_light);
            TextView textView;
            ConstraintLayout.LayoutParams lp;
            int id;
            int idArray[][] = new int[mRows][mCols];
            ConstraintSet cs = new ConstraintSet();
    
            // Add our views to the ConstraintLayout.
            for (int iRow = 0; iRow < mRows; iRow++) {
                for (int iCol = 0; iCol < mCols; iCol++) {
                    textView = new TextView(this);
                    lp = new ConstraintLayout.LayoutParams(ConstraintSet.MATCH_CONSTRAINT,
                                                           ConstraintSet.MATCH_CONSTRAINT);
                    id = View.generateViewId();
                    idArray[iRow][iCol] = id;
                    textView.setId(id);
                    textView.setText(String.valueOf(id));
                    textView.setGravity(Gravity.CENTER);
                    textView.setBackgroundColor(((iRow + iCol) % 2 == 0) ? color1 : color2);
                    layout.addView(textView, lp);
                }
            }
    
            // Create horizontal chain for each row and set the 1:1 dimensions.
            // but first make sure the layout frame has the right ratio set.
            cs.clone(layout);
            cs.setDimensionRatio(R.id.gridFrame, mCols + ":" + mRows);
            for (int iRow = 0; iRow < mRows; iRow++) {
                for (int iCol = 0; iCol < mCols; iCol++) {
                    id = idArray[iRow][iCol];
                    cs.setDimensionRatio(id, "1:1");
                    if (iRow == 0) {
                        // Connect the top row to the top of the frame.
                        cs.connect(id, ConstraintSet.TOP, R.id.gridFrame, ConstraintSet.TOP);
                    } else {
                        // Connect top to bottom of row above.
                        cs.connect(id, ConstraintSet.TOP, idArray[iRow - 1][0], ConstraintSet.BOTTOM);
                    }
                }
                // Create a horiontal chain that will determine the dimensions of our squares.
                // Could also be createHorizontalChainRtl() with START/END.
                cs.createHorizontalChain(R.id.gridFrame, ConstraintSet.LEFT,
                                         R.id.gridFrame, ConstraintSet.RIGHT,
                                         idArray[iRow], null, ConstraintSet.CHAIN_PACKED);
            }
    
            cs.applyTo(layout);
        }
    }
    

    mRows mCols

        2
  •  0
  •   Stakshi    8 年前

    推荐文章