我試圖在textview中顯示代碼片段。但是我無法在textview中水平地自動滾動長文本行。如何在textview中顯示代碼片段?
代碼:
TextView code = new TextView(getActivity());
TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
code.setBackgroundColor(getResources().getColor(android.R.color.black));
code.setPadding(5, 5, 5, 5);
code.setTextColor(getResources().getColor(android.R.color.white));
code.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
code.setClickable(true);
code.setLayoutParams(paramsExample);
setCode(code, R.raw.codesnippet);
ll.addView(code);
我創建這個TextView的在fragment.fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_ll"
android:orientation="vertical">
</LinearLayout>
,我在 coordinatorlayout> nestedscrollview>的LinearLayout開始此片段活動(片段容器)
各人的寬度尺寸爲「搭配父母」
這是setcode方法:
public void setCode(TextView tv, int rawId) {
Resources res = getResources();
BufferedReader input = new BufferedReader(new InputStreamReader(
res.openRawResource(rawId)));
StringBuffer sb = new StringBuffer();
try {
String line;
while ((line = input.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Spanned spannedCode = Html.fromHtml(sb.toString());
tv.setText(spannedCode);
}
我得到從該VIM代碼段(codesnippet.txt):toHtml。
但它不水平滾動。我該怎麼做才能使它像webview水平自動滾動?
後HorizontalScrollView和code.setHorizontallyScrolling(真):
水平滾動工作在單line.I細需要顯示代碼段爲一個塊。
我不知道如果我這樣做是正確的,但也許你沒有在你的文本換行? 通常它是\ n但是你是否正在使用Html.fromHtml,那麼你應該有
換行符。並使用code.setHorizontalScrolling,它應該沒問題。 –