1
我有一个在.xml文件中定义的表,但行和标题必须动态添加。我在填充宽度方面存在问题,在水平和垂直方向上使用表格行。我试过了stretchColumns参数,但它没有帮助。什么是最好的方式来做到这一点?如何在Android中将TableRow宽度设置为max?
table = (TableLayout) findViewById(R.id.testList);
TableRow headerRow = new TableRow(this);
JSONObject obj;
obj = new JSONObject(loadJSONFromAsset());
JSONArray records = obj.getJSONArray("tests");
ArrayList<String> headers = new ArrayList<>();
headers.add("Header 1");
headers.add("Header 2");
headers.add("Header 3");
headers.add("Header 4");
headers.add("Header 5");
for(int i = 0; i < headers.size(); i++) {
TextView header = new TextView(this);
header.setText(headers.get(i));
header.setTextColor(Color.WHITE);
header.setBackgroundColor(Color.rgb(24, 116, 205));
header.setPadding(15, 15, 15, 15);
header.setTextSize(20);
header.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
header.setMaxLines(1);
headerRow.addView(header);
}
table.addView(headerRow);
和XML
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TableLayout
android:id="@+id/testList"
android:layout_height="match_parent"
android:layout_width="wrap_content">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
</GridLayout>
由于您的表格在滚动视图内,因此它没有宽度和高度的限制。因此无法确定哪个是* max *宽度。你只能手动设置 –
@VladMatvienko如何做到这一点没有滚动视图? – Sheppard25
我没有说你不需要scrollView就可以做到。你需要决定哪个宽度是* max *。它是一些确切的值,如100dp,或者它是屏幕宽度,还是什么? –