2017-05-27 60 views
-3

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

enter image description here

我曾尝试加入2多的TextView等于字体大小来实现它。

<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一起滚动?毕竟还有其他方法可以实现吗?

回答

1

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

A 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) 
      } 
     }); 

另一种解决方案是直接在同一个滚动视图中有两个文本视图,也许一些这样的事情:

<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> 

希望这会有所帮助。

+0

但如何添加行号呢?我认为布局不重要 –

+0

你是什么意思?你是在说一个应用程序还是在Android工作室? – Cochi

+0

我已经描述过实现其布局的方式。问题是要知道机制,以便数字textview也滚动与主textview。实现RecyclerView不能解决我的问题。 –

0

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

的getLineCount()的作品的布局已经测量和绘制后,否则返回0。因此,你可以使用的方法,通过以下方式,当布局改变发生在TextViw将通知您:

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