为什么TableRow中的视图不会对其权重做出反应?

时间:2021-08-31 21:54:34

I'm programming an Android app and I'm trying to show a table. My problem is, that the columns don't fill the screen, they just all stick to left.

我正在编写一个Android应用程序,我正在尝试显示一个表。我的问题是,列没有填满屏幕,他们只是坚持左边。

I already tried using padding, but first I don't think that it's compatible with all display resolutions and second the columns were aligned differently when they had different content in them.

我已经尝试过使用填充,但首先我不认为它与所有显示分辨率兼容,其次当列中的内容不同时,列的对齐方式不同。

Then I read about weight and this was exactly what I wanted, giving every column a specific percentage of the width of the screen. But weights did lead to the cells sticking to the left again. They did react in no way to the weights I've set.

然后我读到了重量,这正是我想要的,给每一列特定百分比的屏幕宽度。但是重量导致细胞再次向左侧移动。他们确实没有对我设定的重量做出反应。

I program the view in xml templates and inflate them programmatically using the LayoutInflater. Then I add them to the corresponding TableRow. All that works perfectly fine, but they just don't react to the weights.

我在xml模板中编写视图,并使用LayoutInflater以编程方式对它们进行充气。然后我将它们添加到相应的TableRow中。所有这一切都很好,但他们只是不对重量做出反应。

I would really appreciate your help.

我将衷心感谢您的帮助。

EDIT: The templates refer to a style and look like this:

编辑:模板引用样式,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/scheduleClass" />

And the style looks like this:

风格如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="scheduleItem" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:paddingRight">0dp</item>
        <item name="android:paddingBottom">10dp</item>
    </style>
    <style name="scheduleClass" parent="scheduleItem">
        <item name="android:layout_weight">0.2</item>
    </style>
</resources>

There are other a few other columns, but they just have another name and another weight.

还有其他几个列,但它们只有另一个名称和另一个权重。

And this is how I add the views to the table:

这就是我向表中添加视图的方式:

public void updateList(ArrayList<Lesson> lessons) {
    setContentView(R.layout.schedule);
    TableLayout table = (TableLayout) findViewById(R.id.table);

    for(Lesson cancel : lessons) {
        TableRow row = new TableRow(this);

        TextView date = (TextView) this.getLayoutInflater().inflate(R.layout.scheduledatetemplate, null);
        date.setText(cancel.date);

        TextView classname = (TextView) this.getLayoutInflater().inflate(R.layout.scheduleclasstemplate, null);
        classname.setText(cancel.classname);

        TextView lesson = (TextView) this.getLayoutInflater().inflate(R.layout.schedulelessontemplate, null);
        lesson.setText(cancel.lesson);

        Button details = (Button) this.getLayoutInflater().inflate(R.layout.detailsbuttontemplate, null);
        details.setText(R.string.details);
        details.setOnClickListener(this);

        row.addView(date);
        row.addView(classname);
        row.addView(lesson);
        row.addView(details);

        table.addView(row);
    }
}

And this is the TableLayout xml:

这是TableLayout xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:scrollbars="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">

    </TableLayout>
</ScrollView>

1 个解决方案

#1


0  

You need to declare android:layout_column and android:layout_span attributes to your TableRows

您需要向TableRows声明android:layout_column和android:layout_span属性

See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html

See an example here: http://www.mkyong.com/android/android-tablelayout-example/

请在此处查看示例:http://www.mkyong.com/android/android-tablelayout-example/

UPDATE: Ok, you'll need to set the layout_span and layout_column in each of the rows you're creating, use this addView method: http://developer.android.com/reference/android/widget/TableLayout.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)

更新:好的,你需要在你正在创建的每一行中设置layout_span和layout_column,使用这个addView方法:http://developer.android.com/reference/android/widget/TableLayout.html#addView (android.view.View,int,android.view.ViewGroup.LayoutParams)

To supply the layout params with the appropriate column and span, to make your row span to the entire table, and have the row gravity=center to make their content in the center.

使用适当的列和跨度提供布局参数,使行跨越整个表格,并使行重力=居中以使其内容位于中心。

Also, the table attribute strechColumns http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns Might help you, as in:

此外,表属性strechColumns http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns可能会帮助您,如:

android:strechColumns="*"

or something like that.

或类似的东西。

#1


0  

You need to declare android:layout_column and android:layout_span attributes to your TableRows

您需要向TableRows声明android:layout_column和android:layout_span属性

See http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html

See an example here: http://www.mkyong.com/android/android-tablelayout-example/

请在此处查看示例:http://www.mkyong.com/android/android-tablelayout-example/

UPDATE: Ok, you'll need to set the layout_span and layout_column in each of the rows you're creating, use this addView method: http://developer.android.com/reference/android/widget/TableLayout.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams)

更新:好的,你需要在你正在创建的每一行中设置layout_span和layout_column,使用这个addView方法:http://developer.android.com/reference/android/widget/TableLayout.html#addView (android.view.View,int,android.view.ViewGroup.LayoutParams)

To supply the layout params with the appropriate column and span, to make your row span to the entire table, and have the row gravity=center to make their content in the center.

使用适当的列和跨度提供布局参数,使行跨越整个表格,并使行重力=居中以使其内容位于中心。

Also, the table attribute strechColumns http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns Might help you, as in:

此外,表属性strechColumns http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns可能会帮助您,如:

android:strechColumns="*"

or something like that.

或类似的东西。