【问题标题】:How to shrink a TableLayout column to a minWidth?如何将 TableLayout 列缩小到 minWidth?
【发布时间】:2015-02-27 22:40:07
【问题描述】:

如果第 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?

【问题讨论】:

  • 如何设置每列的权重而不是 android:shrinkColumns android:stretchColumns 。那么对于第 1 列,您可以设置 0.2 的权重,第 2 列的 0.3 和第 3 列的 0.5 等等?
  • @Slartibartfast 到目前为止,您的解决方案是唯一始终确保至少部分显示所有列的解决方法。

标签: android tablelayout


【解决方案1】:

似乎这可能是您需要的实际解决方法,因此请移动我的评论来回答。也许这可以改进,基本上要努力设置这些权重以适合您的应用程序

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

【讨论】:

    【解决方案2】:

    如何将 TableLayout 列缩小到 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'. 
    


    TableLayout中删除android:shrinkColumns="2,3" android:stretchColumns="3"


    解决方案的工作风格和布局:

    //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>
    


    我的测试截图:(纵向和横向)

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

    【讨论】:

    • 如果其他列太大,第 3 列将消失。
    • 所以您的TextView 滑动&lt;item name="android:ellipsize"&gt;marquee&lt;/item&gt; 不起作用?那么你必须为第 3 列的TextView 提供一个固定宽度才能使滑动工作。
    • 你也可以试试所有列有TextView宽度android:layout_weight="1"
    • 是的,已经实现了滑动。您关于重量的建议基本上等于我现在实施的 Slartibartfasts 评论。
    • @OneWorld 我刚刚得到了完美的解决方案,我会尽快更新我的答案
    【解决方案3】:

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

    <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 -->
    

    【讨论】:

    • 您将minWidth应用于第 1 列。但我说的是第 2 列,当第 3 列很大时它会消失。共有 4 列 0、1、2 和 3。
    • 我想我误解了这个问题。您是否试图让所有列都相等?第 2 列是否需要可收缩?据我所知,您可以使用maxWidth 方法,同时将该列设置为可拉伸而不可收缩,或者使用他们建议的重量方法。
    • 我想尽可能有效地利用空间。我认为shrinkColumns 是实现该目标的好属性。但是,shrinkColumnsstretchColumns 可以使列消失。我当然不想要的东西。这就是为什么我尝试将shrinkColumnminWidth 结合起来。但是,minWidth 无法被 TableLayout 识别。这就是我问这个问题的原因。 Slartibartfast 让我走上了正确的道路。他建议避免使用shrinkColumns/stretchColumns 属性,而改用weight。到目前为止,这似乎是满足我需求的最佳解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2014-04-16
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2023-02-24
    相关资源
    最近更新 更多