簡體   English   中英

以編程方式設置ColumnSpan和ColumnWeight

[英]Set ColumnSpan and ColumnWeight programmatically

我一直在嘗試將視圖添加到gridLayout中,但是我沒有按要求調整它們,我曾嘗試用相同的屬性來填充視圖,這些屬性在XML中表現良好,但沒有成功。

現在,我正在嘗試以編程方式創建視圖,但是它不起作用,如何實現所需的視圖?

相關代碼:

這是我的GridLayout

<android.support.v7.widget.GridLayout
            android:id="@+id/grd_team_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:horizontalSpacing="1dp"
            app:columnCount="5"
            app:orientation="horizontal">

            <TextView
                android:id="@+id/txtPlayersNameTi"
                android:text="Jugadores"
                android:textSize="14sp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                app:layout_columnWeight="2"
                app:layout_columnSpan="2"
                android:textStyle="bold"/>
            <TextView
                android:text="Goles"
                android:gravity="center_horizontal"
                android:paddingRight="5dp"
                android:textSize="14sp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                app:layout_columnWeight="1"
                app:layout_columnSpan="1"
                android:textStyle="bold"/>
            <ImageView
                android:src="@drawable/ic_yellow_card_match"
                android:gravity="center_horizontal"
                android:textSize="14sp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                app:layout_columnWeight="1"
                app:layout_columnSpan="1"
                android:textStyle="bold"/>
            <ImageView
                android:src="@drawable/ic_red_card_match"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                app:layout_columnWeight="1"
                app:layout_columnSpan="1"
                android:textStyle="bold"/>
        </android.support.v7.widget.GridLayout>

這是xml預覽中的樣子:

在此處輸入圖片說明

這就是數據的樣子:

在此處輸入圖片說明

但是,在添加以下代碼之后:

int i = 2;
            loading=false;
            GridLayout.Spec col2 = GridLayout.spec(2);
            GridLayout.Spec col3 = GridLayout.spec(3);
            GridLayout.Spec col4 = GridLayout.spec(4);
            GridLayout.Spec colspan2 = GridLayout.spec(0, 1);
            grdTeamAPlayers.setColumnCount(5);
            grdTeamAPlayers.setRowCount(teamAPlayers.size()+2);

            for(TournamentPlayer newPlayer: teamAPlayers)
            {
                GridLayout.Spec row = GridLayout.spec(i);
                GridLayout.LayoutParams first = new GridLayout.LayoutParams(row, colspan2);
                GridLayout.LayoutParams goals = new GridLayout.LayoutParams(row, col2);
                GridLayout.LayoutParams yellow = new GridLayout.LayoutParams(row, col3);
                GridLayout.LayoutParams red = new GridLayout.LayoutParams(row, col4);

                TextView newName = new TextView(myContext);
                newName.setText(newPlayer.getName() + " " + newPlayer.getLastName());
                newName.setTextColor(ContextCompat.getColor(myContext, R.color.black_50));
                newName.setTextSize(14);
                newName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                grdTeamAPlayers.addView(newName, first);


                TextView newGoals = new TextView(myContext);
                newGoals.setText(newPlayer.getGoals() + "");
                newName.setTextSize(14);
                newGoals.setTextColor(ContextCompat.getColor(myContext, R.color.black_50));
                newGoals.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

                TextView newYellow = new TextView(myContext);
                newYellow.setText(newPlayer.getYellowCards() + "");
                newName.setTextSize(14);
                newYellow.setTextColor(ContextCompat.getColor(myContext, R.color.black_50));
                newYellow.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

                TextView newRed = new TextView(myContext);
                newRed.setText(newPlayer.getRedCards()+"");
                newName.setTextSize(14);
                newRed.setTextColor(ContextCompat.getColor(myContext, R.color.black_50));
                newRed.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

                grdTeamAPlayers.addView(newGoals, goals);
                grdTeamAPlayers.addView(newYellow, yellow);
                grdTeamAPlayers.addView(newRed, red);
                i=i+1;
            }

我得到的是:

在此處輸入圖片說明

編輯:

是的,其他標題令人sa目結舌,並且顯示出很大的空白。

因此,嘗試一下並花很多時間最終可以解決這個問題:

  GridLayout.LayoutParams doubleLayoutParams = new GridLayout.LayoutParams();
                    doubleLayoutParams.rowSpec = GridLayout.spec(i,1);
                    //  GridLayout.spec(rowNumber,rowSpan);
                    doubleLayoutParams.columnSpec = GridLayout.spec(0, 2f);
                    //  GridLayout.spec(columnNumber,columnSpan);

 TextView newName =  new TextView(myContext);
                    newName.setText(newPlayer.getName().trim() + " " + newPlayer.getLastName().trim());
                    newName.setTextColor(ContextCompat.getColor(myContext, R.color.black_50));
                    newName.setTextSize(12);
                    newName.setEllipsize(TextUtils.TruncateAt.END);
                    newName.setSingleLine();
                    newName.setWidth(0);
                    grdTeamAPlayers.addView(newName, doubleLayoutParams);

試試這個

對於android:layout_columnWeight

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

在堆棧答案上設置rowSpan或colSpan

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM