Android 项目中是否可以将 imageview 和 textview 放在同一表格行中?

发布于 2024-10-12 13:56:22 字数 1330 浏览 2 评论 0原文

我正在尝试创建一个带有图像视图和文本视图的动态行。 像这样的事情:

|_| 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2024-10-19 13:56:22

您可以在包含 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();

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

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文