动态添加数据行
我正在使用 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));
}
是一个测试,我试图动态地包含一个按钮,如果有效,我将把从 getSensorInfo() 获得的信息放入其中;
但我的活动看起来像这样:
我可能做错了什么?
我找到的所有信息都是这个,但答案没有帮我。
I'm using eclipse + android SDK.
I'm trying to create a table with data provided by sensors. I get the information correctly, by i'm having problems to make a table to show it in XML dinamically.
This is the 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>
And this is the code of my activity:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSensorInfo();
setContentView(R.layout.sensorinfo);
setInfoByView();
}
And
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));
}
THIS IS A TEST, i'm trying to include a button dinamically, if work, i will put the information i get from getSensorInfo();
But my Activity looks like this:
What could I be doing wrong?
All the info i found was THIS, but the answers didn't help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在 XML 文件中创建一个表布局,如下所示:
然后使用此代码在表布局内创建动态行:
最终(测试)解决方案:
按钮现在可见。
First of all create one Table Layout in your XML file like this :
Then use this code for creating dynamic row inside the Table Layout :
FINAL (test) SOLUTION:
And the button is visible now.