首页 > 解决方案 > Android - 如何显示 TextView 的最后一部分

问题描述

当单行文本太长时,如何让 TextView 始终显示它的最后一部分?

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/display_sum"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="0.00"
        android:text=""
        android:textSize="50sp"
        android:textStyle="bold"

        android:layout_gravity="right"
        android:gravity="right"/>
    </HorizontalScrollView>

我要这个:

我要这个

不是这个:

我不想要这个

标签: androidandroid-studiotextview

解决方案


我认为下面的代码可能对您有所帮助。

HorizontalScrollView horizontalScrollView = (HorizontalScrollView) findViewById(R.id.YourIdName)
horizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_FORWARD);
            //Observe for a layout change
            ViewTreeObserver viewTreeObserver = horizontalScrollView .getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        horizontalScrollView .fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                    }
                });
            }
        }

推荐阅读