Is it possible to have imageview and textview in the same table row on Android project?
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 Tex开发者_如何学运维tView(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.
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
<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" />
then you can do something like this: inflate the generic view from the xml file and set the TextView and ImageView context...
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();
Here you can read more information
Hope this helps.
精彩评论