gpt4 book ai didi

Android:以编程方式添加的 TableLayout 中的 View 未显示

转载 作者:行者123 更新时间:2023-11-30 01:30:35 25 4
gpt4 key购买 nike

我正在创建一个应用程序,我需要在其中包含动态表,所以我尝试这样做,并且在大多数情况下,它是有效的。但是我在我的行和列之间有这些分隔线,我使用 Views 来这样做,它们是这样的:

编辑:我认为 View 没有显示,我的第一列是左边框应该在的位置,但不知道这是做什么的。

Dividers appear in the title row

这是我用来生成行的代码:

private void generateMaterialRows(){
TableLayout tableMaterials = (TableLayout) findViewById(R.id.info_table_materials);
//create counter for index in table
int c = 3;
for(Material m: materials){


//makes Rows
TableRow tRow = new TableRow(this);
TableRow tDivider = new TableRow(this);

//Makes 5 bottomBorders and puts them in an Array
View[] bottomBorders = new View[5];

for (int i = 0; i < 5; i++) {
View bottomBorder = new View(this);
bottomBorder.setBackgroundResource(R.color.colorDarkGray);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px
bottomBorder.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
bottomBorder.setLayoutParams(bottomBorder.getLayoutParams());

bottomBorders[i] = bottomBorder;
}

//makes 3 sideBorders and puts them in an Array
View[] sideBorders = new View[3];

for (int i = 0; i < 3; i++){
View sideBorder = new View(this);
sideBorder.setBackgroundResource(R.color.colorDarkGray);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); //converts dp to px
sideBorder.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));
sideBorder.setLayoutParams(sideBorder.getLayoutParams());

sideBorders[i] = sideBorder;
}

//makes columnAmount
TextView txtAmount = new TextView(this);
txtAmount.setBackgroundResource(R.color.colorBlueLight);
txtAmount.setPadding(5,5,5,5);
txtAmount.setText(String.valueOf(m.getAmount()));

//makes columnName
TextView txtName = new TextView(this);
txtName.setBackgroundResource(R.color.colorBlueLight);
txtName.setPadding(5, 5, 5, 5);
txtName.setText(String.valueOf(m.getName()));

//puts content in TableRow
tRow.addView(sideBorders[0]);
tRow.addView(txtAmount);
tRow.addView(sideBorders[1]);
tRow.addView(txtName);
tRow.addView(sideBorders[2]);

//makes divider
for (int i = 0; i < 5; i++){
tDivider.addView(bottomBorders[i]);
}

tableMaterials.addView(tRow, c);
tableMaterials.addView(tDivider, c+1);

c++;
}
}

这是 XML 中的 TableLayout:

                <TableLayout
android:id="@+id/info_table_materials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin">
<!-- Divider Above Title Row -->
<TableRow>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
</TableRow>

<!-- Title Row -->
<TableRow>
<View
android:background="@color/colorDarkGray"
android:layout_width="3px"
android:layout_height="fill_parent"/>
<TextView
android:text="@string/info_amount"
android:background="@color/colorLightGray"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<View
android:background="@color/colorDarkGray"
android:layout_width="3px"
android:layout_height="fill_parent"/>
<TextView
android:text="@string/info_materials"
android:background="@color/colorLightGray"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<View
android:background="@color/colorDarkGray"
android:layout_width="3px"
android:layout_height="fill_parent"/>
</TableRow>

<!-- Divider Between Title and Content Rows -->
<TableRow>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
<View
android:background="@color/colorDarkGray"
android:layout_height="3px"/>
</TableRow>

</TableLayout>

我在 android 方面还很陌生,所以我还在学习,但这让我很困惑,我似乎找不到问题所在。

最佳答案

我发现了问题。我不知道如何使用 LayoutParams 所以我使用了 ViewGroup.LayoutParams 但因为它们在 TableRow 我应该使用 TableRow.LayoutParams

关于Android:以编程方式添加的 TableLayout 中的 View 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794345/

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