[英]Horizontal scroll on textview on android?
I'm working on a calculator.我正在研究计算器。 I noticed that in the default android calc you can scroll the textview horizontally.我注意到在默认的 android calc 中你可以水平滚动 textview。 I looked up the documentation and found out about the attribute android:scrollHorizontally
but after adding it to the textview I still cannot do horizontal scroll, there is no further info about it on the documentation leading me to think that only adding the attr should suffice.我查看了文档并发现了属性android:scrollHorizontally
但在将它添加到 textview 后我仍然无法进行水平滚动,文档中没有关于它的更多信息,这让我认为只添加 attr 就足够了。 This is the calculator's textview:这是计算器的文本视图:
<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" />
When characters exceed the textview width the string is trimmed and ... appear at it's end.当字符超过 textview 宽度时,字符串将被修剪并......出现在它的末尾。 What am I doing wrong?我究竟做错了什么?
<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>
This is how you can make textview scroll horizontally.这就是如何使 textview 水平滚动。
I'm a bit late but I managed to achieve same result without adding the HorizontalScrollView
我有点晚了,但我设法在不添加HorizontalScrollView
情况下实现了相同的结果
EditText
extends TextView
to support scrolling and selection. EditText
扩展了TextView
以支持滚动和选择。 So, you can use the EditText
as a TextView
(touch, focus and cursor are disabled).因此,您可以将EditText
用作TextView
(禁用触摸、焦点和光标)。
<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"/>
I'm just not able to test in a wide range of devices... I'm just sharing because it may be useful to someone else.我只是无法在各种设备上进行测试......我只是分享,因为它可能对其他人有用。
Just use this就用这个
textview.setMovementMethod(new ScrollingMovementMethod());
textview.setHorizontallyScrolling(true);
Probably a late answer but it is possible to make a TextView scroll both ways.可能是一个迟到的答案,但可以使 TextView 双向滚动。 There is no need for the scrollHorizontally
property to be set in the XML or the code.无需在 XML 或代码中设置scrollHorizontally
属性。
The following code makes a single-line or a multi-line TextView scroll both vertically and horizontally based on the text content.以下代码根据文本内容使单行或多行 TextView 垂直和水平滚动。
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:scrollbars="none">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbars="none">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text_content"
fontPath="fonts/roboto_medium.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/primary_text"
android:textSize="14sp" />
</ScrollView>
</HorizontalScrollView>
Note the layout_width
for the ScrollView
and TextView
are set to wrap_content
.注意ScrollView
和TextView
的layout_width
设置为wrap_content
。 The Width for the HorizontalScrollView
can either be wrap_content
or match_parent
and has no effect. HorizontalScrollView
的 Width 可以是wrap_content
或match_parent
并且没有效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.