【发布时间】:2016-01-07 11:15:37
【问题描述】:
我是安卓初学者。 我正在尝试获取当前图像的位置,动态行和列,但我不知道该怎么做。下面是我的代码,请帮助我:
MainActivity extends Activity {
int i,j;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] row = { "ROW1", "ROW2", "Row3"};
String[] column = { "COLUMN1", "COLUMN2","CO" };
int rl=row.length; int cl=column.length;
ScrollView sv = new ScrollView(this);
TableLayout tableLayout = createTableLayout(row, column,rl, cl);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(tableLayout);
sv.addView(hsv);
setContentView(sv);
}
public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
// make it black
textView.setBackgroundColor(Color.BLACK);
}
public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex){
// get row from table with rowIndex
TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);
// get cell from row with columnIndex
TextView textView = (TextView)tableRow.getChildAt(columnIndex);
textView.setText("Hello");
}
private TableLayout createTableLayout(String [] rv, String [] cv, final int rowCount, int columnCount) {
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setBackgroundResource(R.drawable.bookshelf);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.setMargins(2, 2, 2, 2);
tableRowParams.weight = 1;
for (i = 0; i < rowCount; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setBackgroundResource(R.drawable.bookshelf);
for (j= 0; j < columnCount; j++) {
final ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.businessman);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "position : " + rowCount, Toast.LENGTH_SHORT).show();
}
});
tableRow.addView(imageView, tableRowParams);
}
// 6) add tableRow to tableLayout
tableLayout.addView(tableRow, tableLayoutParams);
}
return tableLayout;
}
}
提前致谢。
【问题讨论】:
-
你的位置是什么?或任何其他错误消息?
-
位置 3 点击每个 Image.so 我想要实际位置
-
最好使用
GridView。 -
如何使用gridview 我想要书架视图里面的数据请给我解决方案
-
动态为每一个图像视图设置标签和ID,并尝试获取标签imageview.onClick及其适当的数据或位置。
标签: android dynamic android-tablelayout