【问题标题】:Android: Numbers Shifting in TableRow When Text is UpdatedAndroid:更新文本时 TableRow 中的数字移动
【发布时间】:2013-01-13 18:42:45
【问题描述】:

我有一个 TableLayout,里面有几个 TableRows。在每一行中,我有两个 TextView:第一个显示一些描述性文本,第二个显示一个数字。例如:

| Text |.0.|
| Text2|.0.|
| ...  |.0.|

我有相同权重的 TextView,重力以行本身和每个 TextView 为中心,但是,当在应用程序执行期间更改文本时,元素会移动并且不再排列。所以它变成了这样:

| Text |..0|
| Text2|..0|
| ...  |..0|

这些值不再是它们最初的位置。我希望在文本更改时值保持原样(最长的文本不够长以至于接近数字)。我还想保持格式整洁和尽可能低的 SDK 级别(目前为 8)。

示例 xml 代码:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="8"
    tools:context=".Name" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right" />
    </TableRow>

    // This is the start of where the descriptive text and their values are

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView2-1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />

        <TextView
            android:id="@+id/textView2-2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center" />
     </TableRow>
... // etc

我尝试过更改权重(分配第一个 TextView 权重为 9,第二个权重为 1),但没有奏效。无论文本是什么(包括空字符串“”),我都希望所有数字都排成一行,并且在文本更改时不移位。

有什么想法吗?

【问题讨论】:

    标签: android android-layout android-tablelayout


    【解决方案1】:

    我想你在 docs 中寻找这个

    TableLayout.setShrinkAllColumns(boolean shrinkAllColumns)
    
    TableLayout.setStretchAllColumns(boolean stretchAllColumns)
    

    【讨论】:

    • @lord_sneed 但是您似乎给我们提供了错误的图表 2(1 和 2 相同),我不确定您需要使用哪种方法。在你澄清后我会更新我的答案。
    • 右侧列中的数字在第二列中移动。我不想使用符号来表示空格,因为这可能会更令人困惑。关键是,每次在第一列中更新不同长度的文本时,数字(在第二列中)都会发生变化。无论第一列中的文本更改为什么,我都希望数字列保持固定(不移动)。
    • 我更新了“数字”,所以点代表数字列中的空格。数字根据字符串左右移动(无论是空字符串、短字符串还是相对较长的字符串)。
    • 拉伸列是技巧的一部分。我必须做的另一件事(经过多一点浏览)是将TextViews 的宽度设置为“0dp”。谢谢。出于好奇,您是否对应用(设备到设备)和/或图形进行过任何网络工作?
    • 很高兴有帮助。如果你想要一些一般性的问题,你应该在聊天中问这个。不适合我。
    猜你喜欢
    • 2012-08-08
    • 2017-03-02
    • 1970-01-01
    • 2012-03-04
    • 2015-06-08
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多