我正在使用Eclipse + Android SDK。

我正在尝试创建一个具有传感器提供的数据的表。我正确地获取了信息,因为我有问题要制作一张桌子以在XML上显示餐桌。

这是sensorinfo.xml:

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

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/SensorInfoTableLayout">


<TableRow>

    <TextView
            android:layout_column="1"
            android:text="@string/sensor_name_title"
            android:padding="3dip" />

    <TextView
            android:layout_column="2"
            android:text="@string/sensor_type_title"
            android:padding="3dip" />
    <TextView
            android:layout_column="3"
            android:text="@string/sensor_value_title"
            android:padding="3dip" />
    <TextView
            android:layout_column="4"
            android:text="@string/sensor_unit_title"
            android:padding="3dip" />
</TableRow>

   <View
    android:layout_height="4dip"
    android:background="#FF909090" />

</TableLayout>

这是我活动的代码:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState); 

    getSensorInfo();
    setContentView(R.layout.sensorinfo);

    setInfoByView();

}

    private void setInfoByView()
{
    /* Find Tablelayout defined in xml */
    TableLayout myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);
    /* Create a new row to be added. */
    TableRow myTableRow = new TableRow(this);
    myTableRow.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
    /* Create a Button to be the row-content. */
    Button b = new Button(this);
    b.setText("Dynamic Button");
    b.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
    myTableRow.setBackgroundColor(5);
    /* Add Button to row. */
    myTableRow.addView(b);
    /* Add row to TableLayout. */
    myTableLayout.addView(myTableRow,new TableLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT,
              LayoutParams.WRAP_CONTENT));
}

这是一项测试,我试图在dinam上包含一个按钮,如果有效,我将从getSensorinfo()中获取信息;

但是我的活动看起来像这样:

ACTIVITY IMAGE

我会做什么错?

我发现的所有信息都是 这个, ,但是答案对我没有帮助。

有帮助吗?

解决方案

首先,在XML文件中创建一个表布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#BFD6E8">

<ScrollView android:id="@+id/image_scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TableLayout android:id="@+id/image_table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </TableLayout>

</ScrollView>

</LinearLayout>

然后使用此代码在表布局中创建动态行:

         TableLayout image_table=null;
         image_table=(TableLayout)findViewById(R.id.image_table);

        for(int i=0; i<10; i++){
            TableRow tableRow=new TableRow(this);
            tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
            tableRow.setPadding(5, 5, 5, 5);
            for(int j=0; j<1; j++){
                Button bttn=new Button(this);
                bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                bttn.setText(Hello);
                tableRow.addView(image, 200, 200);
            }
            image_table.addView(tableRow);
        }

最终(测试)解决方案:

private void setInfoByView2()
{
    TableLayout myTableLayout = null;
    TableRow myTableRow = new TableRow(this);
    myTableLayout = (TableLayout)findViewById(R.id.SensorInfoTableLayout);

    myTableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    //myTableRow.setGravity(Gravity.CENTER_HORIZONTAL);
    myTableRow.setPadding(5, 5, 5, 5);
    Button bttn = new Button(this);
    bttn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    bttn.setText("Hello");
    myTableRow.addView(bttn, 200, 200);
    myTableLayout.addView(myTableRow);
}

现在可以看到按钮。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top