gpt4 book ai didi

android - 动态创建 TableRow 然后 RelativeLayout 不显示

转载 作者:行者123 更新时间:2023-11-29 21:58:10 25 4
gpt4 key购买 nike

我正在遍历在 TableRows 中创建 RelativeLayouts 的查询结果。如果我尝试为 RelativeLayout 设置 LayoutParams,则不会出现任何内容,也不会出现任何错误。如果我不这样做,那么所有内容都会出现,但布局设置为 WRAP_CONTENT。我已经尝试了几个例子,但无法让它发挥作用。下面是我的代码:

            TableLayout tblItems = (TableLayout) findViewById(R.id.tblItems);

// Fields from the database (projection)
String[] projection = new String[] { ItemsTable.ITEM_ID,
ItemsTable.ITEM_NAME,
ItemsTable.ITEM_PRICE,
ItemsTable.ITEM_PICTURE };
Cursor cursor = getContentResolver().query(ItemsTable.CONTENT_URI, projection, null, null, null);
startManagingCursor(cursor);

// Establish the item mapping
// String[] columns = new String[] {"name", "price"};
// int[] to = new int[] {R.id.tv_description, R.id.tv_price};

// adapter = new SimpleCursorAdapter(this, R.layout.item_list_select, cursor, columns, to, 0);
// lvSelectItems.setAdapter(adapter);

/*
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndexOrThrow(OrderDetailTable.ORDERDETAIL_PRICE)) {

// String createDate = aCursor.getString(aColumnIndex);
// TextView textView = (TextView) aView;
// textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));

double total = cursor.getDouble(columnIndex);
TextView textView = (TextView) view;
textView.setText("$" + String.format("%.2f",dRound(total, 2)));

return true;
}
return false;
}

});
*/
// tblItems

int totalRows = 0;
int tcolumn = 1;

int numItems = cursor.getCount();
double tempRow = numItems / 2;

if (numItems > 0) {
itemsFound = true;
} else {
itemsFound = false;
}

long iPart; // Integer part
double fPart; // Fractional part
boolean ifFirst = true;

// Get user input
iPart = (long) tempRow;
fPart = tempRow - iPart;

if (fPart > 0) {
totalRows = (int) (iPart + 1);
} else {
totalRows = (int) iPart;
}

TableRow row = null;

cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
// tblItems // TableLayout
// TableRow
// RelativeLayout
// ImageView
// TextView
// TextView

if (ifFirst) {
// create a new TableRow
row = new TableRow(this);
ifFirst = false;
}

// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);


// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

rlp.setMargins(5, 5, 5, 5);

relativeLayout.setPadding(5, 5, 5, 5);
relativeLayout.setBackgroundResource(R.drawable.grpbx_item);

row.addView(relativeLayout, rlp);

// add text view
ImageView imgPic = new ImageView(this);
imgPic.setBackgroundResource(R.drawable.takepic);
imgPic.setPadding(0, 0, 0, 0);
relativeLayout.addView(imgPic);

// add text view
TextView tvName = new TextView(this);
String name = cursor.getString(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_NAME));
tvName.setText("" + name);

// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);

// RelativeLayout.

// Setting the parameters on the TextView
tvName.setLayoutParams(lp);
relativeLayout.addView(tvName);

// add text view
double iPrice = cursor.getDouble(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_PRICE));

TextView tvPrice = new TextView(this);
tvPrice.setText("$" + String.format("%.2f",dRound(iPrice, 2)));
relativeLayout.addView(tvPrice);


tcolumn++;

numItems--;

if (tcolumn == 3) {
// add the TableRow to the TableLayout
tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// create a new TableRow
row = new TableRow(this);
tcolumn = 1;
} else {
if (numItems == 0) {
// add the TableRow to the TableLayout
tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}

// Move to the next record
cursor.moveToNext();
}

}

最佳答案

If I try to set the layout params for the relativeLayout then nothing appears and get no errors. If I don't then everything appears but with the layout being set to wrapcontent.

您为 RelativeLayout 使用了错误的 LayoutParams。您使用:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
row.addView(relativeLayout, rlp);

但由于 RelativeLayout 的父级是 TableRow,您必须使用 TableRow.LayoutParams:

TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.addView(relativeLayout, rlp);

关于android - 动态创建 TableRow 然后 RelativeLayout 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com