0

我正在尝试使用 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();

这段代码的结果是创建两个不同的行,一个带有图像,一个带有 textview。

4

1 回答 1

1

您可以在包含 ImageView 和 TextView 的 xml 文件中编写一个通用视图,稍后在您的循环方法中,您可以扩展视图(以 ImageView 和 TextView 作为子项)并设置它们的内容。

像这样的东西:

在名为 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" />

然后您可以执行以下操作:从 xml 文件中扩充通用视图并设置 TextView 和 ImageView 上下文...

 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();

在这里您可以阅读更多信息

希望这可以帮助。

于 2011-01-18T13:35:04.053 回答