如何创建填充父级并具有相同高度的行的TableLayout?

时间:2016-05-20 10:34:55

标签: android android-layout android-tablelayout

我想创建一个TableLayout来填充它的父级,其中每一行都有相同的高度。如何获得高度相等的行?

2 个答案:

答案 0 :(得分:0)

您可以对布局中的TableRow元素使用layout_weight属性。这将在父高度上平均分配高度(如果您将权重设置为相同)。首先,设置父android:weightSum="1",然后确保各行的所有权重加起来为1

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:weightSum="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- Row 1 -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:padding="5dp" >

        <!-- Your columns here... -->  

    </TableRow>
<!-- Row 2 -->
<TableRow
        android:id="@+id/tableRow2"
        android:layout_weight="0.5"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:padding="5dp" >
     <!-- Your columns here... -->        
    </TableRow>
</TableLayout>

我希望这会有所帮助。请试一试,如果您正在寻找,请告诉我。

答案 1 :(得分:0)

try this:  
 In your layout
 <TableLayout
        android:id="@+id/sub_cat_layout"
        android:layout_width="match_parent"
        android:layout_below="@+id/subcat_headerlayout"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="0dp"
      >
     </TableLayout>

  InActivity
  TableLayout SubCAtLyt = (TableLayout)findViewById(R.id.sub_cat_layout);

   View v1 = null;
        LinearLayout linear_pmain = new LinearLayout(getActivity());
        linear_pmain.setOrientation(LinearLayout.HORIZONTAL);
        LayoutInflater vii = (LayoutInflater)  getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         TableRow tblRow = new TableRow(getActivity());
             tblRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
           arrBookDetails = db.Book( subCatid);
            int catAllLength = arrCatAllBookDetails.size();

           for (int j = 0; j < catAllLength; j++) {

          List<String> lBookDetails = arrBookDetails.get(j);
            //Row value layout
         String book = lBookDetails.get(10);
            v1 = vii.inflate(R.layout.sub_bottomthubnail, null);

                TextView txt = (TextView) v1.findViewById(R.id.txtg1);
                txt.setText( book);
                txt.setTypeface(tface, Typeface.NORMAL);
                 tblRow.addView(v1, j);
    }
        SubCAtLyt.addView(tblRow);