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

时间:2017-08-18 12:43:28

标签: java android

所以问题是这个。我正在编写应用程序,我想展示" java代码"在TextView中,所以当我在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(本)

setOnHightkightListener paint为红色。

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:没找到类" io.github.kbiakov.codeview.CodeView" on path:DexPathList [[zip file" /data/app/tj.horun.las.learnandroidstudio-1/base.apk" ;, zip file" /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" ;, zip文件" / data / app /tj.horun.las.learnandroidstudio-1/split_lib_slice_1_apk.apk" ;, zip文件" /data/app/tj.horun.las.learnandroidstudio-1/split_lib_slice_2_apk.apk" ;, zip文件&# 34; /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&#34 ;, 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)                                                                                      at 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(Native Method)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

3 个答案:

答案 0 :(得分:0)

是的,它可以使用html。 您可以使用Html.fromHtml()来设置java代码和颜色等文本。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以使用CodeView库并为所需的语言设置配置,然后仅使用setText,它将以所需的任何颜色突出显示,并且您可以在运行时更改主题。