代码之家  ›  专栏  ›  技术社区  ›  Victor Laerte

动态生成TableRow时,如何拉伸和收缩列?

  •  2
  • Victor Laerte  · 技术社区  · 13 年前

    我正在使用TableLayout并用自定义行动态填充它,但现在如果我使用android:stretchColumns android:shellikColumns,它就不起作用了。

    我的活动布局:

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <TextView 
            android:text="@string/offline"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/dimGray"
        />
    
        <TableLayout
        android:id="@+id/download_full_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:stretchColumns="2"
        android:shrinkColumns="1"
        >
    </LinearLayout>
    

    我的行布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">
    
        <TableRow 
            android:background="@color/white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*">
    
            <TextView
    
                android:id="@+id/row_name"
                android:text="@string/empty" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7dp"
                android:layout_marginRight="10dp"
                 />
    
            <TextView
                android:id="@+id/row_size"
                android:text="@string/empty" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/lightGray"
                android:layout_marginTop="7dp"
                 />
    
            <Button 
                android:id="@+id/row_btnDownload"
                android:text="@string/baixar"
                android:layout_width="wrap_content" 
                android:layout_height="35dp"
                android:background="@drawable/btn_baixar_verde"
                android:textColor="@color/white"
                android:textSize="13sp"
                android:layout_marginTop="7dp"
                android:layout_marginBottom="7dp"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="15dp"
                android:textStyle="bold"
    
                />
    
        </TableRow>
    
    </LinearLayout>
    

    我的代码:

    TableLayout tableFullDownload = (TableLayout) findViewById(R.id.download_full_table);
    
    LayoutInflater inflater = getLayoutInflater();
    
    ArrayList<MetaDados> fullList = getIntent().getParcelableArrayListExtra("metaDadosFull");
    
    for (MetaDados metaDados : fullList) {
        LinearLayout row = (LinearLayout) inflater.inflate(R.layout.download_table_row, tableFullDownload, false);
        TextView name = (TextView) row.findViewById(R.id.row_name);
        TextView size = (TextView) row.findViewById(R.id.row_size);
    
        name.setText(metaDados.getName());
    
        long fileSize = metaDados.getSize();
        fileSize = (fileSize / 1024) / 1024;
        String sizeToShow = (fileSize + " MB");
    
        size.setText(sizeToShow);
    
        tableFullDownload.addView(row);
    }
    

    我得到的:

    enter image description here

    我想要什么:

    enter image description here

    2 回复  |  直到 13 年前
        1
  •  6
  •   Victor Laerte    13 年前

    问题是我试图拉伸和收缩列,但是,我的行中有一个唯一的视图,在这个视图中我正在创建列,那么解决这个问题的唯一方法就是在视图中使用weight属性

     <TextView
                android:id="@+id/row_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:maxLines="2"
    
        2
  •  0
  •   user3404700    11 年前

    另一种选择是,您可以在res/drawable下创建一个可绘制的资源。 可以是这样的。。。

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@android:color/white"/>
        <stroke android:color="@android:color/black"
            android:width="1dp" />
    </shape>
    

    保存它,然后将其设置为布局中TextView的背景:

    android:background="@drawable/bckgrd_border_textbox"