也許你可以使用兩個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>
希望這會有所幫助。
但如何添加行號呢?我認爲佈局不重要 –
你是什麼意思?你是在說一個應用程序還是在Android工作室? – Cochi
我已經描述過實現其佈局的方式。問題是要知道機制,以便數字textview也滾動與主textview。實現RecyclerView不能解決我的問題。 –