2017-05-27 65 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() ; 
    } 
}); 
相關問題