繁体   English   中英

确定TextView是否需要滚动

[英]Determine if a TextView requires scrolling

CursorAdapterbindView()我将数据绑定到以下布局:

在此处输入图片说明

一个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 ,该行为由“ UP”和“ DOWN” Button s控制。

我想确定TextView需要滚动(足够长的时间以致不能适合其提供的绘图区域),以便可以相应地启用/禁用“ UP”和“ DOWN”按钮。

我目前正在阅读BaseMovementMethodscrollDown函数,正在考虑将其测量逻辑应用于我的适配器,尽管我觉得它应该更简单。 也许是我不知道的内在行为。

除了我建议的方法之外,还有没有更好的方法来实现这一目标?

我会做的就是将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>

在您的活动中,执行以下行:

boolean needScrolling = false;
if(scroller.getHeight() < tv_content.getHeight()) needScrolling = true;

您可以使用静态布局类。 如果使用TextView的参数进行设置,则可以计算渲染文本的高度。

Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;
float spacingMultiplier = 1;
float spacingAddition = 0;
boolean includePadding = false;

StaticLayout myStaticLayout = new StaticLayout(text, myTextView.getPaint(), myTextView.getWidth(), alignment, spacingMultiplier, spacingAddition, includePadding);

float height = myStaticLayout.getHeight(); 

然后,您可以比较文本的高度和TextView的高度,并确定是否需要滚动。

如果myTextView.getPaint()方法不起作用,您也可以尝试使用最小文本大小手动创建Paint对象。

计算没有数据和有数据的mTextView的高度,然后进行比较

mTextView.post(new Runnable() {
        @Override
        public void run() {
            int lineHeight=mTextView.getCompoundPaddingBottom()+ mTextView.getCompoundPaddingTop()+mTextView.getLineHeight();
            int height=mTextView.getHeight()-(mTextView.getCompoundPaddingTop()+mTextView.getLineHeight());
            if (height>lineHeight){

            }
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM