在Android项目中,是否可以将imageview和textview放在同一个表行中?
我正在尝试创建一个带有imageview和textview的动态行。 大概是这样的:在Android项目中,是否可以将imageview和textview放在同一个表行中?,android,dynamic,android-tablelayout,Android,Dynamic,Android Tablelayout,我正在尝试创建一个带有imageview和textview的动态行。 大概是这样的: |_| textview 所以我的问题是,在java源代码中,是否可以在同一行的imageview附近有一个textview,而不使用xml文件 这是我的代码: TableLayout table = (TableLayout)findViewById(R.id.table); db.open(); Cursor c5 = db.getAllCodes(); if
|_| 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();
这段代码的结果是创建两个不同的行,一个带图像,一个带文本视图。您可以在包含ImageView和textview的xml文件中编写一个通用视图,稍后在循环方法中,您可以膨胀视图(ImageView和textview为child)并设置其内容 大概是这样的: 在名为list_item.xml的文件中定义通用视图
希望这能有所帮助。感谢Franco的回复。。我在这一行中得到了错误,在上下文中更具体:LayoutInflater inflater=LayoutInflater.from(上下文);无法解析contextcontext是您的活动上下文,请尝试此LayoutFlater.from(YourActivity.this);
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/icon_search" />
<TextView
android:id="@+id/listItemFirstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:textColor="#FF000000"
android:gravity="center_vertical"
android:text="My Application" />
if (c5.moveToFirst())
{
do{
//rows
TableRow tr = new TableRow(this);
tr.setClickable(true);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
LayoutInflater inflater = LayoutInflater.from(context);
View genericView = inflater.inflate(R.layout.list_item, null);
TextView title = ((TextView)genericView.findViewById(R.id.listItemFirstLine));
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(genericView);
//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();