【问题标题】:Is it possible to have imageview and textview in the same table row on Android project?是否可以在 Android 项目的同一表格行中拥有 imageview 和 textview?
【发布时间】:2011-01-18 13:18:14
【问题描述】:

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

这段代码的结果是创建了两个不同的行,一个是图像,一个是文本视图。

【问题讨论】:

    标签: android dynamic android-tablelayout


    【解决方案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();
    

    Here you can read more information

    希望这会有所帮助。

    【讨论】:

    • 感谢 Franco 的回复。我在这一行中遇到错误,并且在上下文中更具体:LayoutInflater inflater = LayoutInflater.from(context);无法解析上下文
    • context 是你的活动上下文,试试这个 LayoutInflater.from(YourActivity.this);
    猜你喜欢
    • 2011-11-26
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多