在你的项目build.gradle文件中添加maven { url “https://jitpack.io” },注意不是app目录下的build.gradle文件
allprojects {repositories {...maven { url "https://jitpack.io" }}
}
同时 在你app的build.gradle文件中添加compile ‘com.github.daquexian:FlexibleRichTextView:0.8.2’
dependencies {...compile 'com.github.daquexian:FlexibleRichTextView:0.8.2'
}
现在你的Application或者活动中初始化
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import com.daquexian.flexiblerichtextview.FlexibleRichTextView;
import com.daquexian.flexiblerichtextview.Tokenizer;import org.scilab.forge.jlatexmath.core.AjLatexMath;import io.github.kbiakov.codeview.classifier.CodeProcessor;import static com.daquexian.flexiblerichtextview.Tokenizer.setBoldStartLabels;
import static com.daquexian.flexiblerichtextview.Tokenizer.setCenterEndLabels;
import static com.daquexian.flexiblerichtextview.Tokenizer.setCenterStartLabels;
import static com.daquexian.flexiblerichtextview.Tokenizer.setTitleEndLabels;
import static com.daquexian.flexiblerichtextview.Tokenizer.setTitleStartLabels;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);//init flexiblerichtextviewAjLatexMath.init(this);//code highlightCodeProcessor.init(this);FlexibleRichTextView flexibleRichTextView = findViewById(R.id.id_rich_tv);StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("$$化简: \\frac {x+1}{x} - \\frac {1}{x}\n$$");stringBuilder.append("\r\n");stringBuilder.append("$$\\sum_{i=1}^n a_i=0$$,");stringBuilder.append("\r\n");stringBuilder.append("$$f(x)=x^{x^x}$$");stringBuilder.append("\r\n");stringBuilder.append("$$f(x_1,x_x,\\ldots,x_n) = x_1^2 + x_2^2 + \\cdots + x_n^2 $$");stringBuilder.append("\r\n");stringBuilder.append("$$\\left. \\frac{du}{dx} \\right|_{x=0}.$$");stringBuilder.append("\r\n");stringBuilder.append("f(n) = \\begin{cases} \\frac{n}{2}, & \\text{if } n\\text{ is even} \\\\ 3n+1, & " + "\\text{if } n\\text{ is odd} \\end{cases}");stringBuilder.append("\r\n");stringBuilder.append("$$\\mbox{对任意的$x>0$}, \\mbox{有 }f(x)>0. $$");stringBuilder.append("\r\n");stringBuilder.append("$$\\sqrt[n]{x_r_r_r} $$");stringBuilder.append("\r\n");stringBuilder.append("$$ \\frac{x+2}{x} \\sqrt{x} $$");stringBuilder.append("\r\n");stringBuilder.append("$$ \\[f(x,y,z) = 3y^2 z \\left( 3 + \\frac{7x+5}{1 + y^2} \\right).\\] $$");stringBuilder.append("\r\n");stringBuilder.append("$$ P(x|c)=\\frac{P(c|x)\\cdot P(x)}{P(x)} $$");stringBuilder.append("\r\n");stringBuilder.append("$$ \\Large x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a} $$");stringBuilder.append("\r\n");stringBuilder.append("$$ \\sum_{i=1}^n i = \\frac{n(n+1)}2 $$");stringBuilder.append("\r\n");stringBuilder.append("$$ f(x)=\\int_{-\\infty}^x e^{-t^2}dt $$ 这道公式我也不知道怎么做");stringBuilder.append("\r\n");stringBuilder.append("$$ \\cos 2\\theta = \\cos^2 \\theta - \\sin^2 \\theta = 2 \\cos^2 \\theta - 1. $$");stringBuilder.append("\r\n");stringBuilder.append("\r\n");stringBuilder.append("$$ \\frac{x}{2}-3=0 $$");stringBuilder.append("\r\n");stringBuilder.append("$$ x=\\frac{3}{2} $$");stringBuilder.append("\r\n");stringBuilder.append("$$ \\[ \\sum_{k=1}^n k^2 = \\frac{1}{2} n (n+1).\\] $$");flexibleRichTextView.setText(stringBuilder.toString());}
}
在布局文件activity_main.xml中添加
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><com.daquexian.flexiblerichtextview.FlexibleRichTextViewandroid:id="@+id/id_rich_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"></com.daquexian.flexiblerichtextview.FlexibleRichTextView>
</LinearLayout>
运行结果:
如果运行出现以下错误:
需要在gradle.properties中添加下面两行代码
这是因为混合支持库。通过添加这些行选择androidX作为您的支持库
android.useAndroidX=true
android.enableJetifier=true