【问题标题】:Long text in TextView make the layouts to get 'Out of Scope'TextView 中的长文本使布局“超出范围”
【发布时间】:2013-10-29 11:29:46
【问题描述】:

我正面临一个问题,即当TextViews 中的文本变大时,我的布局超出范围

下面是代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Home" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/call_log_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="#00ccff"
            android:clickable="true"
            android:padding="20dp" >

            <TextView
                android:id="@+id/textView19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/call_log"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/call_log_layout"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:background="#993399" >

            <TextView
                android:id="@+id/recent_activities_titles"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/recent_activities"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#ffffff" />

            <TableLayout
                android:id="@+id/recent_activities_table"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/recent_activities_titles" >

                <TableRow
                    android:id="@+id/r_new_lead_row"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp" >

                    <TextView
                        android:id="@+id/textView20"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="@string/r_new_lead"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/new_lead_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="Michell"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />
                </TableRow>

                <TableRow
                    android:id="@+id/r_new_account_row"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp" >

                    <TextView
                        android:id="@+id/textView21"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="@string/r_new_account"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/r_new_account_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="Donna"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp" >

                    <TextView
                        android:id="@+id/textView22"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="@string/r_new_event"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/textView23"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="Meeting with some people I have never seen befpre"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#ffffff"
                        android:textSize="12sp" />
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

</RelativeLayout>

如您所见,有很多嵌套布局,这是因为您只能看到大规模程序的一部分。所以嵌套布局不是问题。

textView23 中,如果我输入“Meeting”之类的小文本,UI 输出就是这样的。

这是正确的,也是我需要的输出。

但是,如果我在其中输入一个大文本,那么输出就是这样的

如您所见,它超出了边界,甚至超过了其他布局的空间。不仅textview23,其他textviews也会出现同样的问题。

现在我的问题是,无论文本有多长,我如何才能将布局保留为第一张图片?

TextView 的Max Width 属性可能不是最佳答案,因为这是针对多个屏幕的,然后会产生很多问题。

也许,如果文本太长,选框?还是仅显示文本视图布局可以显示的内容,并在用户向左滑动文本视图时显示其余部分?还有什么办法吗?我不知道如何使这些工作。

【问题讨论】:

    标签: java android eclipse android-layout textview


    【解决方案1】:

    因为您希望它适用于多种分辨率和尺寸,

    更好的方法是限制文本长度并在末尾显示选取框。

    类似的东西

    android:maxLength="10" 
    android:ellipsize="marquee"
    

    【讨论】:

      【解决方案2】:

      试试这个..

      在您的 ScrollView 中删除 RelativeLayout 替换为 LinearLayout

      <ScrollView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="25dp" >
      
          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
               >
      
      //Your Remaining Content
      
      </LinearLayout>
      

      然后你的RelativeLayoutrelativeLayout4

      作为

      <RelativeLayout
                  android:id="@+id/relativeLayout4"
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="wrap_content"
                  android:layout_alignBaseline="@+id/call_log_layout"
                  android:layout_alignParentRight="true"
                  android:layout_marginTop="10dp"
                  android:background="#993399" >
      

      【讨论】:

      • 您好,感谢您的回复。问题是,在原始代码中,我无法使其成为 LinearLayout
      【解决方案3】:

      这是因为你已经把你的宽度属性 - wrap_content

      首先定义一个固定的宽度,比如 100dp 将属性 maxLine = 1 和 ellipsize = end

      例如,在您的代码中:-

      <TextView
                          android:id="@+id/textView23"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginLeft="10dp"
                          android:text="Meeting with some people I have never seen befpre"
                          android:textAppearance="?android:attr/textAppearanceSmall"
                          android:textColor="#ffffff"
                          android:textSize="12sp" />
      

      使用这个:-

      <TextView
                          android:id="@+id/textView23"
                          android:layout_width="100dp"
                          android:layout_height="wrap_content"
                          android:layout_marginLeft="10dp"
                          android:text="Meeting with some people I have never seen befpre"
                          android:ellipsize="end"
                          android:maxLines="1"    
                          android:singleLine="true"
                          android:textAppearance="?android:attr/textAppearanceSmall"
                          android:textColor="#ffffff"
                          android:textSize="12sp" />
      

      如果您的文本大于 textview 的指定宽度,ellipsize 将在屏幕之间的某个点放置“...”,并且“...”之后的文本将被截断。

      【讨论】:

      • 固定宽度不会这样做。需要随屏幕缩放
      • 您的问题是“无论文本有多长,我如何才能将布局保留为第一张图片?”因此,为此,如果您想要第一个图像以防长文本,则需要将文本缩小。我已经在我的项目中的 50 个不同的地方实现了这个。唯一的解决方案是省略它,这样我的布局就不会被打乱
      猜你喜欢
      • 2020-01-29
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多