Android屏幕上的TextView对象和截断(为多个屏幕设计)

时间:2012-05-23 22:10:56

标签: android android-layout android-widget

这是一个关于如何最好地处理文本截断的简短问题。当你被TextView切断时,你知道向用户显示完整字符串的好方法吗?

任何大小的屏幕都可能会破坏TextView中设置的长字符串的文本。当应用程序支持多种尺寸时,问题变得更加明显,特别是普通和大尺寸屏幕。

一个好的建议是使文本视图可滚动。我对该建议的问题是我已经计划了在所有四个方向上的滑动导航交互,这使文本视图滚动变得复杂。我今天在Android Office时间提出了这个问题。任何建议,特别是基于现有解决方案的建议,都会受到欢迎。提前谢谢......

3 个答案:

答案 0 :(得分:0)

有一个完美的指南,建议将文字大小调整为相应的屏幕尺寸。 Here你可以找到它。

您还可以使用文本视图中的属性;

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
  • 介质
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

答案 1 :(得分:0)

您可以使用此截断文字:

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is your text"
android:ellipsize="marquee" android:lines="1 />

答案 2 :(得分:0)

解决问题的一种方法是在textview中添加一个属性,使其可以滚动而不添加代码:

android:lines="3"
android:textIsSelectable="true" 
android:fadeScrollbars="false"

这是由Android Dev Relations团队成员在办公时间内提出的。但是,在测试时,滚动功能不适用于Android 2.2.2版。我的新解决方案是声明textview可点击并使用onClick回调打开一个对话框。

所以现在我对文本进行了优化(不再使用滚动标记),以便用户清楚地指出文本已被切断。这是xml:

android:lines="3"
android:clickable="true"
android:onClick="showDef"
android:ellipsize="start"
android:isScrollContainer="false"

这是showDef方法:

public void showDef(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("same text as was set in the text view")
        .setCancelable(false)
        .setNegativeButton("Clear", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
        });

AlertDialog alert = builder.create();
alert.show()

在包含textview的视图组具有固定大小的情况下,我比简单滚动更喜欢此解决方案。我希望它对你也有用。