Set ColumnSpan and ColumnWeight programmatically

ⅰ亾dé卋堺 提交于 2019-12-12 02:57:42

问题


I have been trying to add views to a gridLayout, wich I have done, but they're not adjusting to the columns as they should, I had tried inflate the views with the same atribute that is showing well in XML but had no success.

Now, I am trying to create the views programatically but it isn't working, how can I achive what I want?

Relevant Code:

This is my 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>

This is how it looks like in the xml preview:

And this is how it should like with data:

But, after adding with this code:

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;
            }

What I get is this:

EDIT:

Yes, the other titles are dissapering and the big white space is showing.


回答1:


So, Trying a lot, and spending a lot of time finnally work with this:

  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);



回答2:


Try something like this

For android:layout_columnWeight

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

Checkout Set rowSpan or colSpan on Stack answers.



来源:https://stackoverflow.com/questions/35234271/set-columnspan-and-columnweight-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!