这是我在这里的第一个问题。我正在为 Android 开发一个应用程序,并且我正在使用 GridView 作为 DataGrid。我有一些有 7 或 8 列的网格,我需要根据其内容使 gridview 的列具有不同的宽度。我在这里查看了所有帖子,但找不到解决方案。我现在正在做的是设置每个视图的 LayoutParams,计算一个固定宽度,我为每个视图选择一个百分比,视图获得正确的宽度,但第二个视图的文本显示在第一个视图上,依此类推。我所有的视图都是 TextView 或 EditText。
我会感谢你能给我的任何帮助。非常感谢您!
马克西
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int row = GetRow(position);
View field = null;
String text = "";
// convertView = grid.getChildAt(position);
/*f (grid.getChildAt(position) != null) {
if (null == convertView) {
return grid.getChildAt(position);
} else {
convertView = null;
}
}*/
int width = 0;
Point size = new Point();
((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
width = weights[GetCol(position)] * (size.x - 50) / 100;
if (row == 0){
text = localfieldstitles[position];
if (convertView != null){
field = (TextView) convertView;
} else {
field = new TextView(context);
}
((TextView)field).setText(text);
field.setBackgroundResource(R.drawable.buttonblue);
((TextView)field).setTextAppearance(context, android.R.style.TextAppearance_DeviceDefault_Medium);
LayoutParams params = new GridView.LayoutParams(width, LayoutParams.MATCH_PARENT);
field.setLayoutParams(params);
} else {
// Get our text for position
final int col = GetCol(position);
try {
gridData.absolute(row);
if (gridData.getString(remotefields[col]) != null) {
text = gridData.getString(remotefields[col]);
} else {
text = "";
}
if (convertView != null) {
field = convertView;
if (convertView instanceof EditText) {
((EditText) field).setText(text);
} else {
((TextView) field).setText(text);
}
} else {
if (editable[col].equalsIgnoreCase("T")) {
field = new EditText(context);
((EditText) field).setText(text);
((EditText) field).setInputType(fieldtypes[col]);
} else {
field = new TextView(context);
((TextView) field).setText(text);
}
}
if (editable[col].equalsIgnoreCase("T")) {
((EditText)field).setSingleLine();
((EditText)field).setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
try {
if (!isFindColumn(col)) {
gridData.absolute(row);
switch (gridData.getMetaData().getColumnType(col)) {
case Types.FLOAT:
case Types.REAL:
case Types.DECIMAL: gridData.updateFloat(localfields[col], Float.parseFloat(v.getText().toString())); break;
case Types.DOUBLE: gridData.updateDouble(localfields[col], Double.parseDouble(v.getText().toString())); break;
case Types.INTEGER: gridData.updateInt(localfields[col], Integer.parseInt(v.getText().toString())); break;
case Types.VARCHAR:
case Types.NVARCHAR:
case Types.CHAR: gridData.updateString(localfields[col], v.getText().toString()); break;
}
datachange.fieldChange(localfields[col], v.getText().toString());
} else {
findColumn.FINDTEXT = v.getText().toString();
executeSearch();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true; // pass on to other listeners.
}
});
((EditText)field).setOnFocusChangeListener(new EditText.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
try {
if (!isFindColumn(col)) {
gridData.absolute(row);
switch (gridData.getMetaData().getColumnType(col)) {
case Types.FLOAT:
case Types.REAL:
case Types.DECIMAL: gridData.updateFloat(localfields[col], Float.parseFloat(((TextView)v).getText().toString())); break;
case Types.DOUBLE: gridData.updateDouble(localfields[col], Double.parseDouble(((TextView)v).getText().toString())); break;
case Types.INTEGER: gridData.updateInt(localfields[col], Integer.parseInt(((TextView)v).getText().toString())); break;
case Types.VARCHAR:
case Types.NVARCHAR:
case Types.CHAR: gridData.updateString(localfields[col], ((TextView)v).getText().toString()); break;
}
datachange.fieldChange(localfields[col], ((TextView)v).getText().toString());
} /*else {
findColumn.FINDTEXT = ((TextView)v).getText().toString();
executeSearch();
} */
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//do job here owhen Edittext lose focus
}
});
}
LayoutParams params;
if (text.equalsIgnoreCase("")) {
params = new GridView.LayoutParams(width, 40);
} else {
params = new GridView.LayoutParams(width, LayoutParams.WRAP_CONTENT);
}
field.setLayoutParams(params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Finally return the view }
return field;
}