这就是它在开始时的样子。在我创建新项目并添加了一些修改后的代码后,视图会变成这样的smth(注意矩阵中只有那些按钮是黑色的):
以下是我的xml文件包含的内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_container" >
</LinearLayout>
<RelativeLayout
android:layout_below="@id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_centerHorizontal="true"
android:id="@+id/refresh_button"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/refresh"/>
<TextView
android:layout_centerHorizontal="true"
android:layout_below="@id/refresh_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minesLeft"
android:text="@string/minesLeft"/>
<TextView
android:layout_centerHorizontal="true"
android:layout_below="@id/refresh_button"
android:layout_toRightOf="@+id/minesLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/minesLeftCounter"/>
</RelativeLayout>
</RelativeLayout>
这就是我创造这个的方式:
map = new ArrayList< ArrayList<ImageButton> >();
LinearLayout mainContainer = (LinearLayout) c.findViewById(R.id.main_container);
//Params used for cells
TableRow.LayoutParams params = new TableRow.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f
);
int pixelsToDp = convertToPixelsDp(-4.5f);
params.setMargins(pixelsToDp, pixelsToDp, pixelsToDp, pixelsToDp);
//Params used for rows
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
rowParams.setMargins(0, 0, 0, 0);
//Params used for table
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
tableParams.setMargins(0, 0, 0, 0);
//Table generation
TableLayout table = new TableLayout(mAppContext);
table.setLayoutParams(tableParams);
table.setShowDividers(TableLayout.SHOW_DIVIDER_NONE);
table.setStretchAllColumns(true);
for(int i = 0; i < 15; i++){
ArrayList<ImageButton> rowOfList = new ArrayList<ImageButton>();
TableRow row = new TableRow(mAppContext);
row.setShowDividers(TableRow.SHOW_DIVIDER_NONE);
row.setLayoutParams(rowParams);
for(int j = 0; j < 10; j++){
ImageButton btn = new ImageButton(mAppContext);
btn.setPadding(0, 0, 0, 0);
btn.setLayoutParams(params);
btn.setScaleType(ImageView.ScaleType.CENTER_CROP);
rowOfList.add(btn);
row.addView(btn);
}
map.add(rowOfList);
table.addView(row);
}
mainContainer.addView(table);
什么可能导致更改ImageButtons的背景颜色?谢谢你!