Android 项目中是否可以将 imageview 和 textview 放在同一表格行中?
我正在尝试创建一个带有图像视图和文本视图的动态行。 像这样的事情:
|_| textview
所以我的问题是,是否可以在同一行,在java源中的imageview附近有一个textview,而不使用xml文件。
这是我的代码:
TableLayout table = (TableLayout)findViewById(R.id.table);
db.open();
Cursor c5 = db.getAllCodes();
if (c5.moveToFirst())
{
do{
//rows
TableRow tr = new TableRow(this);
tr.setClickable(true);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView title = new TextView(this);
title.setTextColor(Color.BLACK);
title.setTextSize(20);
//TextView text = new TextView(this);
//TextView stamp = new TextView(this);
title.setText(c5.getString(6));
//text.setText(c5.getString(1));
//stamp.setText(c5.getString(2));
//tr.addView(iv);
tr.addView(title);
//tr.addView(text);
//tr.addView(stamp);
//add row to layout
getLayoutInflater().inflate(R.layout.image, table, true);
table.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
while (c5.moveToNext());}
db.close();
此代码的结果是创建两个不同的行,一张带有图像,一张带有文本视图。
I am trying to create a dynamic row with an imageview and a textview.
Something like this:
|_| textview
So my question is if it's possible to have, at the same row, a textview near imageview in the java source, without using xml file..
This is my code:
TableLayout table = (TableLayout)findViewById(R.id.table);
db.open();
Cursor c5 = db.getAllCodes();
if (c5.moveToFirst())
{
do{
//rows
TableRow tr = new TableRow(this);
tr.setClickable(true);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView title = new TextView(this);
title.setTextColor(Color.BLACK);
title.setTextSize(20);
//TextView text = new TextView(this);
//TextView stamp = new TextView(this);
title.setText(c5.getString(6));
//text.setText(c5.getString(1));
//stamp.setText(c5.getString(2));
//tr.addView(iv);
tr.addView(title);
//tr.addView(text);
//tr.addView(stamp);
//add row to layout
getLayoutInflater().inflate(R.layout.image, table, true);
table.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
while (c5.moveToNext());}
db.close();
The result for this code is to create two different rows, one with the image and one with the textview.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在包含 ImageView 和 TextView 的 xml 文件中编写一个通用视图,稍后在循环方法中您可以膨胀该视图(将 ImageView 和 TextView 作为子视图)并设置其内容。
像这样的事情:
在名为 list_item.xml 的文件中定义通用视图
那么你可以这样做:从 xml 文件中扩充通用视图并设置 TextView 和 ImageView 上下文...
在这里您可以阅读更多信息
希望这会有所帮助。
you can write a generic View in xml file that contains the ImageView and the TextView, later in your loop method you can inflate the View (with ImageView and TextView as childs) and setting their contents.
Something like this:
define a generic view in a file called list_item.xml
then you can do something like this: inflate the generic view from the xml file and set the TextView and ImageView context...
Here you can read more information
Hope this helps.