【问题标题】:Android table layout formatting problems not fitting to screenAndroid表格布局格式问题不适合屏幕
【发布时间】:2013-02-05 01:23:01
【问题描述】:

我正在使用带有 3 列标签、* 和微调器的表格布局,所有这些都有 wrap_content 并且当微调器文本很小时它工作正常。如果任何一个微调器的文本很长,那么所有微调器都会延伸到最后,而且它也不适合屏幕。

有没有办法让每个微调器适应其文本大小,例如 tablelayout 中的 wrap_content。 用于表格布局示例的 xml。

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:stretchColumns="2" >
      <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_column="0"
                android:gravity="right"
                android:paddingLeft="3dip"
                android:text="Sold To"
                android:textColor="#000033"
                android:textSize="17sp" />

            <TextView
                android:layout_column="1"
                android:gravity="left"
                android:text="*"
                android:textColor="#FF0000"
                android:textSize="17sp"
                android:width="20dip" />

            <Spinner
                android:id="@+id/sold_to_dd"
                android:layout_width="wrap_content"
                android:layout_column="2"
                 />
        </TableRow>


    </TableLayout>

我希望很多人会遇到同样的问题。提前致谢

【问题讨论】:

  • 你能粘贴你的布局文件吗?

标签: android android-spinner android-tablelayout


【解决方案1】:

您可以为每个微调器设置maxWidth 属性,或者您可以检查字符串的长度,即放置在微调器中以防万一它的长度 > x(比如 10)然后使用子字符串 fn 只取 (x- 3) 字符串中的字符并以“...”作为后缀并将该字符串放入微调器中。例如:StringABCDEFGHIJKLM 可以变成 StringAB...

【讨论】:

  • 谢谢你能把代码贴出来,在哪里检查那个条件
  • 您必须对要添加到微调器中的每个字符串执行子字符串操作。如果您使用 ArrayList 填充微调器,请在将每个字符串添加到微调器之前对每个字符串执行子字符串操作
【解决方案2】:

这是因为您仅拉伸作为微调器的第 2 列。您必须在每一列上设置权重。类似的东西

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" >
  <TableRow>

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_column="0"
            android:gravity="right"
            android:paddingLeft="3dip"
            android:text="Sold To"
            android:textColor="#000033"
            android:textSize="17sp" />

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_column="1"
            android:gravity="left"
            android:text="*"
            android:textColor="#FF0000"
            android:textSize="17sp" />

        <Spinner
            android:id="@+id/sold_to_dd"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:layout_column="2"
            android:singleLine="true"
             />
    </TableRow>


</TableLayout>

权重表示要占据的屏幕百分比。所以每个 textView 将占据屏幕的 20%,而 spinner 将占据 60%。

你也可以参考这个:Android Holo theme does not wrap multiple line spinner dropdown items

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 2017-07-05
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2013-04-28
    相关资源
    最近更新 更多