【问题标题】:TextView not always expanding to a second line in TableLayoutTextView 并不总是扩展到 TableLayout 中的第二行
【发布时间】:2014-04-18 22:07:20
【问题描述】:

我有一个应用程序正在从 json 文件中提取信息:

{
 "EVENTS": [
        {
            "what": " TABLE-QUIZ",
            "who": "anyone",
            "when": "31st January",
            "where": "ABC HELLO"
        }
    ]    
}

当我运行应用程序时,有时When 列中的文本在进入第二行时会在较小的屏幕上被截断。如果What 列中的文本太大,它将进入第二行并且不会被截断。为什么When 列没有进入第二行,而What 列在?

这是我正在使用的布局xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*" >

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

    <TextView
        android:id="@+id/what"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/textlines"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="12sp" >

    </TextView>

    <TextView
        android:id="@+id/when"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="0.65"
        android:background="@drawable/textlines"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="12sp" >
    </TextView>

    <TextView
        android:id="@+id/where"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/textlines"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="12sp" >
    </TextView>

    <TextView
        android:id="@+id/forWho"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/textlines"
        android:clickable="true"
        android:gravity="center"
        android:onClick="onClick"
        android:textColor="#fff"
        android:textSize="12sp" >
    </TextView>
</TableRow>

</TableLayout>

编辑: 如果我添加:

android:minHeight="30dp"

TextView 然后我可以让它显示这两行,但我希望有一个可以自动工作的解决方案。

【问题讨论】:

  • 嗨,豪利!对不起打扰!您介意重新打开4-years-old question。这个问题缺乏的替代答案很少。这些答案值得放心,并且可能对 AOSP 初学者特别有帮助。提前致谢!

标签: android android-layout textview android-tablelayout


【解决方案1】:

问题是您将TableLayout's width(父母的宽度)设置为wrap_content。像这样将其设置为 match_parent

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

使When 列转到下一行而不被切断。

【讨论】:

    【解决方案2】:

    你需要查看完整的数据意味着在Textview上添加这个

    android:singleLine="false" 
    

    希望这篇文章对你有所帮助。

    【讨论】:

      【解决方案3】:

      将所有TextView中的android:layout_weight改为1,android:layout_width改为0dp,这样layout_weight="1"属性就可以工作了。

      希望这会有所帮助!

      【讨论】:

      • 确实可以,但是每列的宽度相同,我需要When 列比其他列小。
      【解决方案4】:

      希望对你有帮助

      <?xml version="1.0" encoding="utf-8"?>
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/tableLayout1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:shrinkColumns="*"
          android:stretchColumns="*" >
      
          <TableRow
              android:id="@+id/tableRow1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal" >
      
              <TextView
                  android:id="@+id/textView41"
                  android:text="when"
                  android:textStyle="bold" />
      
              <TextView
                  android:id="@+id/textView42"
                  android:gravity="center_horizontal"
                  android:text="28°C" />
      
              <TextView
                  android:id="@+id/textView43"
                  android:gravity="center_horizontal"
                  android:text="where" />
      
              <TextView
                  android:id="@+id/textView44"
                  android:gravity="center_horizontal"
                  android:text="what" />
      
              <TextView
                  android:id="@+id/textView44"
                  android:gravity="center_horizontal"
                  android:text="for who" />
      
          </TableRow>
      </TableLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 2015-02-28
        • 2014-03-17
        • 2020-06-27
        • 2021-11-07
        • 1970-01-01
        相关资源
        最近更新 更多