以编程方式设置ColumnSpan和ColumnWeight [英] Set ColumnSpan and ColumnWeight programmatically

查看:1772
本文介绍了以编程方式设置ColumnSpan和ColumnWeight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图添加一个gridLayout的视图,我已经做了,但是他们并没有按照他们应该的方式调整列,我已经尝试用同样的赞美夸张的视图,这在XML中表现很好,但是没有成功。



现在,我正在以编程方式创建视图,但它不起作用,我该怎么办? b

相关代码



这是我的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:horizo​​ntalSpacing =1dp
app:columnCount =5
app:orientation = 水平 >

< 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 = 黑体/>
< TextView
android:text =Goles
android:gravity =center_horizo​​ntal
android:paddingRight =5dp
android:textSize =14sp
android:paddingTop =5dp
android:paddingBottom =5dp
app:layout_columnWeight =1
app:layout_columnSpan =1
android :TEXTSTYLE = 黑体/>
< ImageView
android:src =@ drawable / ic_yellow_card_match
android:gravity =center_horizo​​ntal
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_horizo​​ntal
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; 
load = 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);

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

我得到的是这样的:





编辑:



是的,其他标题是消除了大白色的空间。

解决方案

所以,
尝试很多,花费大量的时间在这里工作:

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


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.

解决方案

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

这篇关于以编程方式设置ColumnSpan和ColumnWeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆