【问题标题】:Multiline textview in table row not wrapping properly表格行中的多行文本视图未正确换行
【发布时间】:2015-08-13 17:28:56
【问题描述】:

我试图让表格行中的文本视图的文本出现在多行上,但由于某种原因它没有正确换行,我最终得到了这个(见截图)。我知道文本视图可以做到这一点,但我不知道是什么导致了这个问题的发生。有什么办法可以解决这个问题?

截图

列表项 XML

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:singleLine="false"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
</TableLayout>

【问题讨论】:

  • 给出一些正确的填充,为什么你没有为&lt;TableRow&gt;设置属性?
  • @Rustam Cool,但它不适用于平板电脑。检查适当的屏幕截图。
  • 是横向模式吗?如果是这样,那么在 layout-land 文件夹中为横向创建单独的布局

标签: android xml textview android-tablelayout


【解决方案1】:

您需要指定android:layout_weight="1"

这个值告诉TableRow TextView 需要等同于match_parent

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <TableRow
            >
        <ImageView
                android:id="@+id/img"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"/>

        <TextView
                android:id="@+id/txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="15dp"
                android:layout_marginEnd="15dp"
                android:layout_weight="1"
                android:text="This is a long long long long text fodshfkjsdkjfjsh js hdfjksh kjdhf kjshdkjf"
                android:textAppearance="?android:attr/textAppearanceLarge"/>
    </TableRow>
</TableLayout>

【讨论】:

  • 我想如果你使用weightlayout_width应该是0dp
  • 如果您在TableRow 中的LinearLayout 中嵌入您的视图,您需要仅在LinearLayoutTextView 上设置android:layout_weight="1" 并在TextView 上设置android:layout_width="match_parent"跨度>
【解决方案2】:

&lt;TableRow&gt;属性

   <TableRow
       android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:singleLine="false"
            android:paddingRight="10dip"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

【讨论】:

    【解决方案3】:

    试试下面的代码可能会有所帮助

    <com.project_name.JustifiedTextView.JustifiedTextView
                        android:id="@+id/txtHide"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:padding="25dp"
                        xmlns:noghteh="http://noghteh.ir" 
                        android:text="" 
                        android:textColor="@color/BlueText"
                        android:textSize="@dimen/text_size" />
    

    链接: https://github.com/navabi/JustifiedTextView

    【讨论】:

      【解决方案4】:

      简单的解决方法是增加边距, 试试:

      <TextView
              android:id="@+id/txt"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:layout_marginStart="15dp"
              android:layout_marginEnd="30dp"
              android:singleLine="false"
              android:textAppearance="?android:attr/textAppearanceLarge" />
      

      【讨论】:

        猜你喜欢
        • 2017-12-15
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        • 2014-06-22
        相关资源
        最近更新 更多