如何将 TableLayout 列缩小到 minWidth?

How to shrink a TableLayout column to a minWidth?

如果第 3 列的文本较大,则第 2 列消失。如果我设置 android:stretchColumns="2,3",第 2 列也会消失。我想将 minWidths 设置为第 2 列。但是这个属性被忽略了。

我现在正在通过设置 android:stretchColumns="3"android:shrinkColumns="3" 并将 maxWidth 应用于位置 2 上的 TextView 来帮助自己。这几乎可以完成工作,但 minWidth 将是更多解决方案。

            <TableLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:shrinkColumns="2,3"
                android:stretchColumns="3" >

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/TextViewIdLabel"
                        style="@style/overlay_content"
                        android:text="@string/id_label"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/TextViewIdValue"
                        style="@style/overlay_content"
                        android:layout_marginLeft="4dp" />

                    <TextView
                        android:id="@+id/TextViewPersonIdLabel"
                        style="@style/overlay_content"
                        android:layout_marginLeft="8dp"
                        android:text="@string/person_id_label"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/TextViewPersonIdValue"
                        style="@style/overlay_content"
                        android:layout_marginLeft="4dp" />
                </TableRow>
                <!-- I skipped the other rows -->
           </TableLayout>

这是使用的样式,如果你想知道:

<style name="overlay_content">
    <item name="android:textColor">@color/foreground_color_on_dark_background</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">20sp</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:singleLine">true</item>
</style>

更新:

随着答案的出现,我发现了更多关于我在这条评论中总结的要求:

How to shrink a TableLayout column to a minWidth?

Actually it is simple, just TextView layout_width="wrap_content" can do
that, the reason not working in your style is that 'wrap_content' should
not work together with TextView 'ellipsize', they are conflict. So you need
one seperate syle 'overlay_slide' for the column that has long text.

So use 'overlay_content' for columns those have short text, use    
'overlay_slide' for columns that have long text.

Besides I suppose the 'ellipsize' in your style is not working because
seems two items missing:

    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>

finally in the column that has long text, you also need:

    android:layout_weight="1"

to use all rest of space for 'ellipsize'. 


Remove android:shrinkColumns="2,3" android:stretchColumns="3" from TableLayout


Solution working styles and layout:

//remove 'ecllipze' related items from 'overlay_content' 
<style name="overlay_content">
   <item name="android:textColor">@color/foreground_color_on_dark_background</item>
   <item name="android:layout_width">wrap_content</item>
   <item name="android:layout_height">wrap_content</item>
   <item name="android:textSize">20sp</item>    
</style>

//remove 'android:layout_width' from 'overlay_slide'
<style name="overlay_slide">
   <item name="android:textColor">@color/foreground_color_on_dark_background</item>
   <item name="android:layout_height">wrap_content</item>
   <item name="android:textSize">20sp</item>
   <item name="android:ellipsize">marquee</item>

   <item name="android:focusable">true</item>
   <item name="android:focusableInTouchMode">true</item>

   <item name="android:singleLine">true</item>
</style>


<TableLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"

 >

   <TableRow
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <TextView
           android:id="@+id/TextViewIdLabel"
           style="@style/overlay_content"              
           android:text="@string/id_label"
           android:textStyle="bold" />

      <TextView
           android:id="@+id/TextViewIdValue" 
           style="@style/overlay_content"
           android:layout_marginLeft="4dp"/>

      <TextView
           android:id="@+id/TextViewPersonIdLabel"
           style="@style/overlay_content"
           android:layout_marginLeft="8dp"
           android:text="@string/person_id_label"
           android:textStyle="bold"/>

      <TextView
           android:id="@+id/TextViewPersonIdValue"
           style="@style/overlay_slide"
           android:layout_weight="1" 
           android:layout_marginLeft="4dp" />  

   </TableRow>
</TableLayout>


Screenshot from my testing: (Portrait and Landscape)

My test long text column is text 'Column 1 ... Column 8'

您不能以这种方式更改列的宽度。宽度仅由该列中最宽的行定义。如果那里没有显示,就不会有宽度。您可以通过调整该行的文本视图来进行调整。

<TextView
            android:id="@+id/TextViewIdValue"
            style="@style/overlay_content"
            android:layout_marginLeft="4dp"
            android:minEms="4"              <!--    You can use either of these -->
            android:minWidth="40dp" />

这里的权衡是您可能 运行 空间不足,而您的其他列将 运行 超出末尾,但您可以调整文本大小以更正该问题,即使您必须用 android:textSize="10dp"

覆盖你的风格

这段代码似乎对我有用,但我还将您的 TableLayout 宽度从“0dp”更改为 "match_parent"。

  <TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:shrinkColumns="3"
android:stretchColumns="*" >

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextViewIdLabel"
        style="@style/overlay_content"
        android:text="id_label"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/TextViewIdValue"
        style="@style/overlay_content"
        android:layout_marginLeft="4dp"
         />

    <TextView
        android:id="@+id/TextViewPersonIdLabel"
        style="@style/overlay_content"
        android:layout_marginLeft="8dp"
        android:text="person_id_label"
        android:textSize="10dp"
        android:textStyle="bold"
        android:minEms="4"
        android:minWidth="40dp" />
    <!-- You can use either of these -->

    <TextView
        android:id="@+id/TextViewPersonIdValue"
        style="@style/overlay_content"
        android:layout_marginLeft="4dp" />
</TableRow>
<!-- I skipped the other rows -->

看来这可能是您需要的实际解决方法,因此请将我的评论移至答案中。也许这可以改进,但本质上要努力设置这些权重以适合您的应用

如何设置每列权重而不是 android:shrinkColumnsandroid:stretchColumns 。因此,对于第 1 列,您可以设置 0.2 权重,第 2 列为 0.3,第 3 列为 0.5,依此类推