Java 为什么我的TableRow中的视图不';你对他们的体重没有反应吗?

Java 为什么我的TableRow中的视图不';你对他们的体重没有反应吗?,java,android,android-tablelayout,Java,Android,Android Tablelayout,我正在编写一个Android应用程序,并试图显示一个表格。我的问题是,这些列并没有填满屏幕,它们只是都停留在左边 我已经尝试过使用填充,但首先我不认为它与所有显示分辨率兼容,其次,当列中有不同的内容时,列的对齐方式也不同 然后我读到了关于重量的内容,这正是我想要的,给每一列指定屏幕宽度的百分比。但重量确实导致细胞再次贴向左侧。他们对我设定的重量没有任何反应 我用xml模板编程视图,并使用LayoutFlater以编程方式对其进行膨胀。然后我将它们添加到相应的TableRow中。所有这些都很好,但

我正在编写一个Android应用程序,并试图显示一个表格。我的问题是,这些列并没有填满屏幕,它们只是都停留在左边

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

然后我读到了关于重量的内容,这正是我想要的,给每一列指定屏幕宽度的百分比。但重量确实导致细胞再次贴向左侧。他们对我设定的重量没有任何反应

我用xml模板编程视图,并使用LayoutFlater以编程方式对其进行膨胀。然后我将它们添加到相应的TableRow中。所有这些都很好,但它们对重量没有反应

我非常感谢你的帮助

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

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

样式如下所示:

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

0dp
包装内容
0dp
10dp
0.2
还有其他一些列,但它们只是有另一个名称和另一个权重

下面是我如何将视图添加到表中的:

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);
    }
}
public void updateList(ArrayList课程){
setContentView(R.layout.schedule);
TableLayout table=(TableLayout)findviewbyd(R.id.table);
用于(课程取消:课程){
TableRow row=新的TableRow(本);
TextView日期=(TextView)this.getLayoutFlater().inflate(R.layout.scheduledatetemplate,null);
date.setText(取消日期);
TextView类名称=(TextView)this.getLayoutFlater().inflate(R.layout.scheduleclasstemplate,null);
classname.setText(取消.classname);
TextView课程=(TextView)this.getLayoutFlater().inflate(R.layout.schedulelessontemplate,null);
lesson.setText(取消.lesson);
按钮详细信息=(按钮)this.getLayoutFlater().inflate(R.layout.detailsbuttontemplate,null);
details.setText(R.string.details);
details.setOnClickListener(this);
row.addView(日期);
row.addView(类名);
row.addView(课程);
row.addView(详细信息);
表.addView(行);
}
}
这是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>

您需要向您的
表格行
声明
android:layout\u列
android:layout\u span
属性

请参见此处的示例:

更新: 好的,您需要在要创建的每一行中设置layout_span和layout_列,请使用此addView方法: ,int,android.view.ViewGroup.LayoutParams)

为布局参数提供适当的列和跨距,使行跨距到整个表,并使行重心=中心,使其内容位于中心

此外,表属性
strechColumns
可能对您有所帮助,例如:

android:strechColumns="*"

或者类似的东西。

谢谢你,但我不太明白你的意思。我现在为每一列添加了android:layout\u column属性,但它们仍然停留在左侧。我也不想将我的视图跨多个列。请将TableLayout xml与TableRows一起发布,这样我就可以帮你了。谢谢你,视图对我设置的权重仍然没有任何反应,但android:StretchCluns=“*”会导致我想要的结果。我认为TableLayout根本没有使用权重,并不是所有的布局都考虑大小的权重,例如RelayalayOutt忽略它们。如果我的回答有帮助,请将其标记为解决方案,以便其他人可以将其用作解决方案well@iturki没有要发布的布局,因为我以编程方式添加了TableRows及其内容,但我在主发布中添加了这样做的代码。