gpt4 book ai didi

java - 如何在textview中设置文本并将其格式化为代码? (Android Java)

转载 作者:行者123 更新时间:2023-12-02 12:21:26 25 4
gpt4 key购买 nike

所以问题是这样。我正在编写应用程序,并且想在TextView中显示“ java代码”的一部分,因此当我在TextView中设置Text时,它看起来像一个简单的文本。

是否可以显示带有所有标签颜色的代码之类的“代码”等?

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import br.tiagohm.codeview.Language;
import br.tiagohm.codeview.Theme;
import io.github.kbiakov.codeview.CodeView;


/**
* A simple {@link Fragment} subclass.
*/
public class FragmentActivityTop extends Fragment implements br.tiagohm.codeview.CodeView.OnHighlightListener {
CodeView mCodeView;
int tag=0;
public FragmentActivityTop() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//CodeProcessor.init(getContext());
View v =inflater.inflate(R.layout.activity_fragment_top, container, false);

//CodeView codeView = (CodeView) v.findViewById(R.id.code_viewjava);

String code = " @Override\n" +
" public View onCreateView(LayoutInflater inflater, ViewGroup container,\n" +
" Bundle savedInstanceState) {\n" +
" // Inflate the layout for this fragment\n" +
" View v = inflater.inflate(R.layout.fragment_xml, container, false);\n" +
"\n" +
" WebView webView = (WebView)v.findViewById(R.id.webfragxml);";


mCodeView = (CodeView)v.findViewById(R.id.codeView);

mCodeView.setOnHighlightListener(this)
.setOnHighlightListener(this)
.setTheme(Theme.AGATE)
.setCode(code)
.setLanguage(Language.JAVA)
.setWrapLine(true)
.setFontSize(14)
.setZoomEnabled(true)
.setShowLineNumber(true)
.setStartLineNumber(9000)
.apply();

return v;
}


@Override
public void onStartCodeHighlight() {

}

@Override
public void onFinishCodeHighlight() {

}

@Override
public void onLanguageDetected(Language language, int i) {

}

@Override
public void onFontSizeChanged(int i) {

}

@Override
public void onLineClicked(int i, String s) {

}
}


这是我尝试执行的片段代码:
mCodeView.setOnHighlightListener(this)

setOnHightkightListener绘制为红色。

08-19 18:30:37.308 17929-17929/tj.horun.las.learnandroidstudio D/AndroidRuntime: Shutting down VM


--------- beginning of crash


08-19 18:30:37.314 17929-17929 / tj.horun.las.learnandroidstudio E / AndroidRuntime:FATAL EXCEPTION:main
                                                                                 流程:tj.horun.las.learnandroidstudio,PID:17929
                                                                                 android.view.InflateException:二进制XML文件第8行:二进制XML文件第8行:膨胀类io.github.kbiakov.codeview.CodeView时出错
                                                                                 原因:android.view.InflateException:二进制XML文件第8行:错误膨胀了类io.github.kbiakov.codeview.CodeView
                                                                                 原因:java.lang.ClassNotFoundException:在路径:DexPathList [[zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/base”中找不到类“ io.github.kbiakov.codeview.CodeView” .apk”,zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_dependencies_apk.apk”、zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_0_apk.apk”压缩文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_1_apk.apk”,压缩文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_2_apk.apk”,压缩文件“ / data / app / tj.horun.las.learnandroidstudio-1 / split_lib_slice_3_apk.apk”,zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_4_apk.apk”、zip文件“ / data / app / tj.horun.las.learnandroidstudio-1 / split_lib_slice_5_apk.apk”,zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_6_apk.apk”、zip文件“ /data/app/tj.horun”。 las.learnandroidstudio-1 / split_lib_slice_7_apk.apk”,zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice” _8_apk.apk”,zip文件“ /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_9_apk.apk”],nativeLibraryDirectories=[/data/app/tj.horun.las.learnandroidstudio-1/lib/arm64 ,/ system / lib64,/ vendor / lib64]]
                                                                                     在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                     在java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                                     在java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                                     在android.view.LayoutInflater.createView(LayoutInflater.java:609)
                                                                                     在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                                                     在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                                     在android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                                     在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                                     在android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                                     在android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                                     在tj.horun.las.learnandroidstudio.LayoutFragment.onCreateView(LayoutFragment.java:28)
                                                                                     在android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
                                                                                     在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
                                                                                     在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
                                                                                     在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
                                                                                     在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
                                                                                     在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
                                                                                     在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
                                                                                     在android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
                                                                                     在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2034)
                                                                                     在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
                                                                                     在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                                     在android.support.v4.view.ViewPager.populate(ViewPager.java:1239)
                                                                                     在android.support.v4.view.ViewPager.populate(ViewPager.java:1087)
                                                                                     在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:270)
                                                                                     在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874)
                                                                                     在android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                                     在android.view.Choreographer.doFrame(Choreographer.java:618)
                                                                                     在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                                     在android.os.Handler.handleCallback(Handler.java:751)
                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     在android.os.Looper.loop(Looper.java:241)
                                                                                     在android.app.ActivityThread.main(ActivityThread.java:6274)
08-19 18:30:37.315 17929-17929 / tj.horun.las.learnandroidstudio E / AndroidRuntime:at java.lang.reflect.Method.invoke(本机方法)
                                                                                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

您可以使用this library启用代码突出显示。

关于java - 如何在textview中设置文本并将其格式化为代码? (Android Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45756980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com