2
我正在使用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));
}
这是一个测试,我是试图包括一个按钮dinamically,如果工作,我会把我从getSensorInfo()获得的信息;
但我的活动是这样的:
什么可能我是做错了什么?
我发现的所有信息是THIS,但答案并没有帮助我。
感谢您的帮助,调整您的代码,我的作品! – vgonisanz 2012-01-10 14:11:53
@Piperoman永远欢迎。享受! – Venky 2012-01-10 14:14:10