2015-05-08 118 views
0

我有一个叫做行布局文件,该文件基本上是我多么希望我的行看起来像编程放置布局视图表格视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TableRow 
     android:id="@+id/tableRow" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 

     <TextView 
      android:id="@+id/oid" 
      android:layout_width="0dp" 
      android:layout_weight="1" android:background="#dcdcdc" 
      android:textColor="#000000" 
      android:padding="5dip" 

      /> 
     <TextView 
      android:id="@+id/value" 
      android:layout_width="0dp" 
      android:layout_weight="1" android:background="#d3d3d3" 
      android:textColor="#000000" 
      android:padding="5dip" 

      /> 
     <TextView 
      android:id="@+id/type" 
      android:layout_width="0dp" 
      android:layout_weight="1" android:background="#dcdcdc" 
      android:textColor="#000000" 
      android:padding="5dip" 

      /> 
    </TableRow> 
</LinearLayout> 

一个包装里面现在ID希望能够创建一个行使用这个布局并将其放置在我的表格中,该表格在碎片布局中定义。这是我当前创建一行的代码(它不使用行布局文件)并将其添加到表中,但该行没有格式化,因为我喜欢它。

TableLayout varbindTable = (TableLayout) view2.findViewById(R.id.tableLayout1); 
TableRow row = new TableRow(getActivity()); 

row.setLayoutParams(new TableRow.LayoutParams(
     TableRow.LayoutParams.MATCH_PARENT, 
     TableRow.LayoutParams.MATCH_PARENT)); 
varbindTable.addView(row); 
TextView name = new TextView(getActivity()); 

name.setText(((EditText)view.findViewById(R.id.oid)).getText().toString()); 
name.setTextColor(Color.BLACK); 
name.setLayoutParams(new TableRow.LayoutParams(
     0, 
     TableRow.LayoutParams.MATCH_PARENT)); 
TextView value = new TextView(getActivity()); 

value.setText(((EditText)view.findViewById(R.id.value)).getText().toString()); 
value.setTextColor(Color.BLACK); 
value.setLayoutParams(new TableRow.LayoutParams(
     0, 
     TableRow.LayoutParams.MATCH_PARENT)); 
TextView type = new TextView(getActivity()); 

type.setText(((Spinner)view.findViewById(R.id.data_type_spinner)).getSelectedItem().toString()); 
type.setTextColor(Color.BLACK); 
type.setLayoutParams(new TableRow.LayoutParams(
     0, 
     TableRow.LayoutParams.MATCH_PARENT)); 

row.addView(name); 
row.addView(value); 
row.addView(type); 

回答

0

那么,乍一看,这个问题看起来像你的设置是不同的2个例子。在xml中,您正在使用layout_width =“0dp”和layout_weight =“1”。但是,在您使用MATCH_PARENT的代码中。

我能找到的最近的例子: How do you setLayoutParams() for an ImageView?

+0

我读它,我已经做了回答暗示什么。代码部分是部分无关的,因为我想将行布局注入到表中而不是构建,然后在代码中添加行。 (也许使用适配器?)除非我可以通过setLayoutParams实现布局。 –

+0

我最终通过向setLayoutParams添加一个权重来获得正确的布局 –