在安卓上的文本视图上水平滚动?

2022-09-01 10:21:46

我正在研究计算器。我注意到,在默认的Android计算中,您可以水平滚动文本视图。我查找了文档并发现了有关该属性的信息,但是在将其添加到textview之后,我仍然无法进行水平滚动,文档中没有关于它的进一步信息,导致我认为仅添加attr就足够了。这是计算器的文本视图:android:scrollHorizontally

    <TextView android:id="@+id/edit_text"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight=".8"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:gravity="center|right"
        android:text="0" />

当字符超过文本视图宽度时,字符串将被修剪并且...出现在它的末尾。我做错了什么?


答案 1
<HorizontalScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:scrollHorizontally="true"
        android:text="Horizontal scroll view will work now"/>

</HorizontalScrollView>

这就是使文本视图水平滚动的方法。


答案 2

我有点晚了,但我设法在没有添加的情况下取得了相同的结果HorizontalScrollView

EditText扩展以支持滚动和选择。因此,您可以将 用作(触摸,焦点和光标被禁用)。TextViewEditTextTextView

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" --> This remove that line at the bottom
    android:clickable="false" --> It can be true if you need to handle click events
    android:cursorVisible="false" --> Hide the cursor
    android:focusable="false" --> Disable focus
    android:singleLine="true"
    android:text="This is a very long text that won't be possible to display in a single line"/>

我只是无法在各种设备上进行测试...我只是分享,因为它可能对其他人有用。


推荐