当表格行中的layout_height = wrap_content时,内容不打包

问题描述:

我创建了一个具有不同长度文本的简单表格。我在我的表格行中使用了layout_height=wrap_content。如果您看到图片,则会在下一个元素和前一个元素之间留出较宽的空间。如果我设置了高度,那么在风景中观看时空间将保持不变。我不知道在哪里修复它。我主要使用风格......所以很奇怪,有些工作,有些不是。当表格行中的layout_height = wrap_content时,内容不打包

sample app

sampleLayout.xml

<TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="90dp" 
     android:background="@color/form_background" 
     android:shrinkColumns="*" 
     android:stretchColumns="*"> 

     <!--Table header--> 
     <TableRow 
      style="@style/table_row" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/th_component" 
       style="@style/table_header" 
       android:layout_height="match_parent" 
       android:layout_weight="2.8" 
       android:text="@string/tableH_komponen"/> 

      <TextView 
       android:id="@+id/th_marks" 
       style="@style/table_header" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="@string/tableH_markah"/> 

      <TextView 
       android:id="@+id/th_demerits" 
       style="@style/table_header" 
       android:layout_height="match_parent" 
       android:layout_weight="0.7" 
       android:text="@string/tableH_demerit"/> 

      <TextView 
       android:id="@+id/th_notes" 
       style="@style/table_header" 
       android:layout_height="match_parent" 
       android:layout_weight="0.5" 
       android:text="@string/tableH_catatan"/> 
     </TableRow> 

     <!--Component C1--> 

     <TableRow 
      style="@style/table_row" 
      android:layout_height="wrap_content"> 

      <TextView 
       style="@style/table_attr" 
       android:text="@string/c1"/> 

      <TextView 
       android:id="@+id/tv_cC1" 
       style="@style/table_component" 
       android:text="@string/cC1" 
       /> 

      <TextView 
       android:id="@+id/tv_mc1" 
       style="@style/table_marks" 
       android:layout_height="wrap_content" 
       android:text="@string/_6"/> 

      <CheckBox 
       android:id="@+id/checkBox_c1" 
       style="@style/table_demerit" 
       android:layout_height="wrap_content"/> 

      <ImageButton 
       android:id="@+id/btn_c1" 
       style="@style/table_notes" 
       android:layout_height="wrap_content" 
       android:text="@string/catatan"/> 
     </TableRow> 

     <!--Component C2--> 

     <TableRow 
      style="@style/table_row" 
      android:layout_height="wrap_content"> 

      <TextView 
       style="@style/table_attr" 
       android:text="@string/c2"/> 

      <TextView 
       android:id="@+id/tv_cC2" 
       style="@style/table_headComponent" 
       android:text="@string/cC2"/> 

      <ImageButton 
       android:id="@+id/btn_c2" 
       style="@style/table_notes" 
       android:layout_height="wrap_content" 
       android:text="@string/catatan"/> 
     </TableRow> 

     <TableRow 
      style="@style/table_row" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/tv_cC2_1" 
       style="@style/table_subComponent" 
       android:text="@string/cC2_1"/> 

      <TextView 
       android:id="@+id/tv_mC2_1" 
       style="@style/table_marks" 
       android:layout_height="match_parent" 
       android:text="@string/_1"/> 

      <CheckBox 
       android:id="@+id/checkBox_c2_1" 
       style="@style/table_subDemerit" 
       android:layout_height="wrap_content"/> 
     </TableRow> 

     <TableRow 
      style="@style/table_row" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/tv_cC2_2" 
       style="@style/table_subComponent" 
       android:layout_height="wrap_content" 
       android:text="@string/cC2_2"/> 

      <TextView 
       android:id="@+id/tv_mC2_2" 
       style="@style/table_marks" 
       android:layout_height="wrap_content" 
       android:text="@string/_1"/> 

      <CheckBox 
       android:id="@+id/checkBox_c2_2" 
       style="@style/table_subDemerit" 
       android:layout_height="wrap_content"/> 
     </TableRow> 
    </TableLayout> 

</FrameLayout> 

的strings.xml

<resources> 
<!--Case C--> 
    <string name="cC1">Pemeriksaan kesihatan ke atas semua pengendali makanan: 
    \n - Mendapat suntikan pelalian anti-tifoid 
    \n - Menghadiri Latihan Pengendali Makanan </string> 
    <string name="cC2">Tahap kebersihan diri yang baik:</string> 
    <string name="cC2_1">- Berpakaian bersih dan bersesuaian</string> 
    <string name="cC2_2">- Memakai apron yang bersih dan berpenutup kepala</string> 
</resources> 

styles.xml

<resources> 

    <!--kpkt table header--> 

    <style name="table_header"> 
     <item name="android:textSize">@dimen/textSize_subheading</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textColor">@color/table_textcolor</item> 
     <item name="android:background">@color/table_background</item> 
     <item name="android:padding">@dimen/text_padding</item> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:gravity">center</item> 
    </style> 

    <!--table style--> 

    <style name="table_row"> 
     <item name="android:gravity">end</item> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:weightSum">5</item> 
</style> 

    <style name="table_attr"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_weight">0.3</item> 
     <item name="android:gravity">center</item> 
    </style> 

    <style name="table_component"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_weight">2.5</item> 
     <item name="android:paddingTop">8dp</item> 
     <item name="android:paddingBottom">8dp</item> 
    </style> 

    <style name="table_marks"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_weight">1</item> 
     <item name="android:gravity">center</item> 
     <item name="android:padding">8dp</item> 
    </style> 

    <style name="table_demerit"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_weight">0.7</item> 
     <item name="android:gravity">center_horizontal</item> 
     <item name="android:visibility">visible</item> 
    </style> 

    <style name="table_notes"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_weight">0.5</item> 
     <item name="srcCompat">@drawable/ic_edit</item> 
     <item name="android:contentDescription">catatan</item> 
     <!--<item name="android:drawableBottom">@drawable/ic_edit</item>--> 
     <!--<item name="android:drawableTop">@drawable/ic_edit</item>--> 
    </style> 

    <!--kpkt table head component--> 

    <style name="table_headComponent"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_height">40dp</item> 
     <item name="android:padding">@dimen/text_padding</item> 
     <item name="android:layout_weight">4.2</item> 
    </style> 

    <!--kpkt table subcomponent--> 

    <style name="table_subDemerit"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_weight">1.2</item> 
     <item name="android:visibility">visible</item> 
    </style> 

    <style name="table_subComponent"> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:layout_height">40dp</item> 
     <item name="android:padding">8dp</item> 
     <item name="android:layout_weight">2.5</item> 
    </style> 

</resources> 

我可以建议你理解wrap_content和match_parent的概念,以获得更好的解决方案。

我认为你可以使用水平和垂直方向的线性布局。

由于线性布局更灵活,并且比表格布局和表格行具有更好的性能。

问:什么时候应该使用表格布局和表格行?答案:当我们有一个类似的数据类型,并且我们想在行和列视图中表示时,我们应该使用表格布局和表格行。

但是,如果我们有非线性数据,那么我们不应该使用表布局和表行...我认为你有非线性数据。

为了更好地理解以非线性形式显示数据,您应该知道线性布局的weight_sum和layout_weight属性。