我面临的问题是,一旦 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
,其他 textview
也会出现同样的问题。
现在我的问题是,无论文本有多长,如何才能将布局保持为第一张图片?
TextView 的Max Width
属性可能不是最好的答案,因为这是针对多个屏幕的,并且会带来很多问题。
也许,如果文字太长,可以将其字幕显示出来吗?或者只显示 TextView 布局可以显示的内容,并在用户向左滑动 TextView 时显示其余内容?还有其他办法吗?我不知道如何使这些工作发挥作用。
最佳答案
当您希望它适用于多种分辨率和尺寸时,
更好的方法是限制文本长度并在末尾显示选取框。
类似这样的
android:maxLength="10"
android:ellipsize="marquee"
关于java - TextView中的长文本使布局得到 'Out of Scope',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19657023/