Android 以编程方式将 LinearLayout 添加到 tableRow
我正在以编程方式填充 tableLayout。我希望我的行是这样的
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/textView2" android:text="TextView"
android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
我已经通过代码尝试过这样做
TableRow row = new TableRow(activity);
row.setGravity(Gravity.CENTER);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
image.setPadding(10, 10, 10, 10);
((LinearLayout) image).setGravity(Gravity.LEFT);
row.addView(image);
LinearLayout main = new LinearLayout(row.getContext());
main.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
main.setOrientation(LinearLayout.VERTICAL);
TextView nameView = new TextView(activity);
if(name.length() > 22)
nameView.setText(name.substring(0, 22)+"...");
else
nameView.setText(name);
nameView.setPadding(10, 10, 10, 10);
nameView.setTextColor(Color.parseColor("#83be56"));
nameView.setTypeface(null,Typeface.BOLD);
nameView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
main.addView(nameView,new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
row.addView(main);
bt.setText("Respond");
bt.setPadding(10,10,10,10);
bt.setGravity(Gravity.CENTER);
row.addView(bt);
但是线性布局没有出现在行中。我哪里错了?? 谢谢
I'm populating a tableLayout programmatically. I want that my row are like this
<TableRow android:id="@+id/tableRow1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/textView2" android:text="TextView"
android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
I've tried it by code doing this
TableRow row = new TableRow(activity);
row.setGravity(Gravity.CENTER);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
image.setPadding(10, 10, 10, 10);
((LinearLayout) image).setGravity(Gravity.LEFT);
row.addView(image);
LinearLayout main = new LinearLayout(row.getContext());
main.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
main.setOrientation(LinearLayout.VERTICAL);
TextView nameView = new TextView(activity);
if(name.length() > 22)
nameView.setText(name.substring(0, 22)+"...");
else
nameView.setText(name);
nameView.setPadding(10, 10, 10, 10);
nameView.setTextColor(Color.parseColor("#83be56"));
nameView.setTypeface(null,Typeface.BOLD);
nameView.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
main.addView(nameView,new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
row.addView(main);
bt.setText("Respond");
bt.setPadding(10,10,10,10);
bt.setGravity(Gravity.CENTER);
row.addView(bt);
But the linearlayout doesn't appear in the row. Where i'm going wrong??
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会看看这个SO答案,它解释了如何以编程方式创建
TableRow
,并从部分XML
定义中扩充它。编辑,评论后:链接答案的要点是:
You might have a look at this SO answer, which explains how you can programmatically create a
TableRow
, inflating it from a partialXML
definition.EDIT, after comments: The gist of linked answer is:
以下是我如何以编程方式将 TableRows 添加到表布局中。每个视图问题实际上是一个独立于它所放置的片段的 XML 文件。该片段有一个 Tablelayout、questionContainer,它接受每个新问题并将其添加到底部(questionContainer 位于滚动视图中):
Here's how I add TableRows to a table layout, programmatically. Each View question is actually an XML file independent of the fragment it gets placed in. That fragment has a Tablelayout, questionContainer, that takes each new question and adds it to the bottom (the questionContainer is in a scrollview):