【问题标题】:How to add number line before multiline Textview in Android?如何在Android中的多行Textview之前添加数字行?
【发布时间】:2017-05-27 05:22:55
【问题描述】:

我最近看到一个 Android 应用程序,其中在单独的 Column 中的每一行之前添加了数字行。主列也与多行 TextView 一起滚动。如何将这些行号与 textview 一起添加?看截图:

我尝试通过添加 2 个具有相同字体大小的多行文本视图来实现它。

 <LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/ct"
        android:layout_weight="0.075"
        android:textColor="@android:color/darker_gray"
        android:layout_width="0dp"
        android:layout_marginRight="1dp"
        android:background="#303133"
        android:layout_height="match_parent"
        android:inputType="textMultiLine"
        android:textSize="15sp"/>
    <ScrollView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#303133">
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/txt"
                android:textColor="@android:color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:textSize="15sp" />

        </HorizontalScrollView>

    </ScrollView>

</LinearLayout>

但是如何根据其他 Textview 在 NumberLine TextView 中添加相同数量的行?以及如何滚动 NumberLine TextView 以及其他 Textview ?毕竟还有其他方法可以实现吗?

【问题讨论】:

    标签: java android android-layout android-studio textview


    【解决方案1】:

    也许您可以使用两个RecyclerView,一个用于行号,另一个用于代码,其中每一行都是一个项目。

    例如tutorial

    编辑

    我认为您可以为您的文本使用第二个滚动视图,例如:

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">
        <ScrollView
            android:id="@+id/scroll_view_ct"
            android:layout_width="0dp"
            android:layout_weight="0.075"
            android:layout_height="match_parent"
            android:background="#303133">
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                     android:id="@+id/ct"
                     android:layout_weight="match_parent"
                     android:textColor="@android:color/darker_gray"
                     android:layout_width="0dp"
                     android:layout_marginRight="1dp"
                     android:background="#303133"
                     android:layout_height="match_parent"
                     android:inputType="textMultiLine"
                     android:textSize="15sp"/>
           </HorizontalScrollView>
        </ScrollView>
        <ScrollView
            android:id="@+id/scroll_view_txt"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:background="#303133">
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/txt"
                    android:textColor="@android:color/white"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:textSize="15sp" />
    
            </HorizontalScrollView>
    
        </ScrollView>
    
    </LinearLayout>
    

    然后协调滚动监听器

    scrollViewCT.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                scrollViewTxt.scrollTo(scrollx, scrollY)
            }
        });
    
    scrollViewTxt.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                @Override
                public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    scrollViewCT.scrollTo(scrollx, scrollY)
                }
            });
    

    另一种解决方案是将两个文本视图直接放在同一个 scrollview 中,可能是这样的:

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="match_parent">
        <ScrollView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:background="#303133">
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                     android:layout_width="match_parent"
                     android:orientation="horizontal"
                     android:layout_height="match_parent">
                <TextView
                    android:id="@+id/ct"
                    android:layout_weight="0.075"
                    android:textColor="@android:color/darker_gray"
                    android:layout_width="0dp"
                    android:layout_marginRight="1dp"
                    android:background="#303133"
                    android:layout_height="match_parent"
                    android:inputType="textMultiLine"
                    android:textSize="15sp"/>
                <TextView
                    android:id="@+id/txt"
                    android:textColor="@android:color/white"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:gravity="end"
                    android:textSize="15sp" />
                </LinearLayout>
            </HorizontalScrollView>
    
        </ScrollView>
     </LinearLayout>
    

    希望这会有所帮助。

    【讨论】:

    • 但是如何在其中添加行号呢?我认为布局无关紧要
    • 什么意思?您是在为应用程序还是在 android studio 中发言?
    • 我已经描述了实现其布局的方法。问题是要知道机制,以便数字 textview 也与主 textview 一起滚动。实现 RecyclerView 并不能解决我的问题。
    【解决方案2】:

    您可以使用 TextView 中的 getLineCount() 来获取当前文本视图在屏幕上显示的实际行数。

    getLineCount()在布局被测量和绘制后工作,否则返回0。所以你可以使用下面的方法,它会在TextViw发生布局变化时通知你:

    textView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // call textview.getLineCount() ;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2011-02-19
      • 1970-01-01
      • 2012-04-16
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多