大佬教程收集整理的这篇文章主要介绍了android – 如何在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); 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);
我在fragment.fragment_layout.xml中创建了这个textview:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://scheR_532_11845@as.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:toHtml获得了代码片段(codesnippet.txt).
但它不会水平滚动.我应该怎样做才能使它像webview一样自动滚动?
在HorizontalScrollView和code.setHorizontallyScrolling(true)之后:
<horizontalscrollview android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="40dp" android:layout_height="wrap_content" android:scrollHorizontally="true" android:text="Horizontal scroll view"/> </horizontalscrollview>
以上是大佬教程为你收集整理的android – 如何在textview中显示代码片段?全部内容,希望文章能够帮你解决android – 如何在textview中显示代码片段?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。