【问题标题】:Android TextView with scrollbars and maxHeight带有滚动条和 maxHeight 的 Android TextView
【发布时间】:2011-01-29 12:49:04
【问题描述】:

我需要设置 TextView 的最大高度(使用 maxHeight 或 maxLines)。如果有更多的文本行,则应显示滚动条。我应该为此使用什么标记?

最初的想法是用ScrollView包裹TextView,但是ScrollView没有maxHeight属性。

【问题讨论】:

    标签: android


    【解决方案1】:

    布局:

    <TextView
        android:id="@+id/text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:maxLines="3"
        android:scrollbars="vertical"
        android:textColor="@android:color/secondary_text_dark_nodisable"
        >
    </TextView>
    

    代码:

    TextView textView = (TextView)findViewById(R.id.text_view);
    textView.setMovementMethod(ScrollingMovementMethod.getInstance());
    

    注意:

    android:textColor="@android:color/secondary_text_dark_nodisable" 用于避免textView被触摸时文本淡出。

    【讨论】:

    • 当我尝试这个时,TextView 会显示滚动条,但我实际上无法让它滚动。
    • 你必须从代码中执行这个:TexView.setMovementMethod(new ScrollingMovementMethod());
    • 我使用了这种方法,因为我的椭圆不能超过两行。谢谢。
    • @alex2k8 4 年后你的回答帮助了我。浪费了一个小时弄清楚这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 2011-05-10
    • 1970-01-01
    相关资源
    最近更新 更多