在 CursorAdapter
的 bindView()
内部,我将数据绑定(bind)到以下布局:
一个 TextView
和两个 Button
:“UP”和“DOWN”。
TextView
在 XML 中定义如下:
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="25dp"
android:scrollbars="vertical"
android:textAlignment="textStart"
android:textColor="#5c6284"
app:autoSizeMaxTextSize="40sp"
app:autoSizeMinTextSize="20sp"
app:autoSizeTextType="uniform" />
垂直滚动行为应用于 TextView
,该行为由“向上”和“向下”按钮
控制。
我想确定 TextView
是否需要滚动(足够长,不适合其提供的绘图区域),以便我可以相应地启用/禁用“向上”和“向下”按钮。
我目前正在阅读 BaseMovementMethod
的 scrollDown
函数,考虑将其测量逻辑应用到我的适配器中,尽管我感觉它应该简单得多。也许是我不知道的内置行为。
除了我建议的方法之外,还有更好的方法来实现这一目标吗?
最佳答案
我要做的是将 TextView 放入 ScrollView 中,如下所示:
<ScrollView
android:id="@+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test texts here"/>
</ScrollView>
在您的 Activity 中,执行以下行:
boolean needScrolling = false;
if(scroller.getHeight() < tv_content.getHeight()) needScrolling = true;
关于java - 确定 TextView 是否需要滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49577272/