我一直在尝试向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;
}
我得到的是:
修改
是的,其他游戏正在消失,并且正在显示大的空白区域。
答案 0 :(得分:4)
所以, 尝试了很多,花了很多时间最终完成这个工作:
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);
答案 1 :(得分:2)
尝试这样的事情
对于 android:layout_columnWeight
((GridLayout.LayoutParams) this.getLayoutParams()).columnSpec =
GridLayout.spec(GridLayout.UNDEFINED, 1f);
在Stack答案中结帐Set rowSpan or colSpan。