Android Rich Text Editor中的CSS样式无效

时间:2015-07-11 17:21:41

标签: android html css

在将docx内容转换为html并通过webview阅读时,我一直在使用Docx4j库读取Docx文件。但是,当我尝试在Android Rich Text Editor中加载时,相同的HTML代码我没有获得任何格式的文本和图像。我发现css样式已被注释,所以我使用下面的代码使用字符串html删除了它:

html = html.replace("<!--", "");
html = html.replace("-->", "");

这也没有解决我的问题,我收到的是没有格式的相同纯文本。然而,html包含定义良好的css代码。快照

我检查过html在RTEditor中有效:

代码:

String subject = "";Spanned description = null;
    String message = null;
    String signature = null;String withCharacters;
    String tsx="";String html = null;
    if (savedInstanceState == null) {
        Intent intent = getIntent();
        subject = getIntent().getStringExtra("textReportFileName");
        message = getStringExtra(intent, "message");
        signature = getStringExtra(intent, "signature");
        mUseDarkTheme = intent.getBooleanExtra("mUseDarkTheme", false);
        mSplitToolbar = intent.getBooleanExtra("mSplitToolbar", false);
    } else {
        subject = savedInstanceState.getString("subject", "");
        mUseDarkTheme = savedInstanceState.getBoolean("mUseDarkTheme", false);
        mSplitToolbar = savedInstanceState.getBoolean("mSplitToolbar", false);
    }

    final long startTime = System.currentTimeMillis();
    final long endTime;
    try {
        final LoadFromZipNG loader = new LoadFromZipNG();
        WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage)loader.get(new FileInputStream(new File(Environment.getExternalStorageDirectory()+"/"+getIntent().getStringExtra("textReportFileName"))));
        //getIntent().getStringExtra("textReportFileName")
        String IMAGE_DIR_NAME = "images";

        String baseURL = this.getDir(IMAGE_DIR_NAME, Context.MODE_WORLD_READABLE).toURL().toString();
        System.out.println(baseURL); // file:/data/data/com.example.HelloAndroid/app_images/

        // Uncomment this to write image files to file system
        ConversionImageHandler conversionImageHandler = new AndroidFileConversionImageHandler( IMAGE_DIR_NAME, // <-- don't use a path separator here
                baseURL, false, this);

        // Uncomment to use a base 64 encoded data URI for each image
        // ConversionImageHandler conversionImageHandler = new AndroidDataUriImageHandler();

        HtmlExporterNonXSLT withoutXSLT = new HtmlExporterNonXSLT(wordMLPackage, conversionImageHandler);

        html = XmlUtils.w3CDomNodeToString(withoutXSLT.export());
        html = html.replace("<!--", "");
        html = html.replace("-->", "");
        withCharacters = StringEscapeUtils.unescapeHtml(html);
        description = Html.fromHtml(withCharacters);

        //WebView webview = (WebView)this.findViewById(R.id.webpage);
        //webview.loadDataWithBaseURL(baseURL, html , "text/html", null, null);


        // TabHost mTabHost = getTabHost();
        // mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Web Page").setContent(R.id.webpage));
        // mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("View Source").setContent(tv.getId()));
        // mTabHost.setCurrentTab(0);

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(),"Invalid Format Exception",Toast.LENGTH_SHORT).show();
    } finally {
        endTime = System.currentTimeMillis();
    }
    final long duration = endTime - startTime;
    System.err.println("Total time: " + duration + "ms");


    message = String.valueOf(description);

    Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    // set theme
    setTheme(mUseDarkTheme ? R.style.ThemeDark : R.style.ThemeLight);

    super.onCreate(savedInstanceState);

    // set layout
    setContentView(mSplitToolbar ? R.layout.rte_demo_2 : R.layout.rte_demo_1);

    // initialize rich text manager
    RTApi rtApi = new RTApi(this, new RTProxyImpl(this), new RTMediaFactoryImpl(this, true));
    mRTManager = new RTManager(rtApi, savedInstanceState);

    ViewGroup toolbarContainer = (ViewGroup) findViewById(R.id.rte_toolbar_container);

    // register toolbar 0 (if it exists)
    HorizontalRTToolbar rtToolbar0 = (HorizontalRTToolbar) findViewById(R.id.rte_toolbar);
    if (rtToolbar0 != null) {
        mRTManager.registerToolbar(toolbarContainer, rtToolbar0);
    }

    // register toolbar 1 (if it exists)
    HorizontalRTToolbar rtToolbar1 = (HorizontalRTToolbar) findViewById(R.id.rte_toolbar_character);
    if (rtToolbar1 != null) {
        mRTManager.registerToolbar(toolbarContainer, rtToolbar1);
    }

    // register toolbar 2 (if it exists)
    HorizontalRTToolbar rtToolbar2 = (HorizontalRTToolbar) findViewById(R.id.rte_toolbar_paragraph);
    if (rtToolbar2 != null) {
        mRTManager.registerToolbar(toolbarContainer, rtToolbar2);
    }

    // set subject
    mSubjectField = (EditText) findViewById(R.id.subject);
    mSubjectField.setText(subject);

    // register message editor
    mRTMessageField = (RTEditText) findViewById(R.id.rtEditText_1);
    mRTManager.registerEditor(mRTMessageField, true);
    if (message != null) {
        mRTMessageField.setRichTextEditing(true, message);
    }

    // register signature editor
    mRTSignatureField = (RTEditText) findViewById(R.id.rtEditText_2);
    mRTManager.registerEditor(mRTSignatureField, true);
    if (signature != null) {
        mRTSignatureField.setRichTextEditing(true, signature);
    }

    mRTMessageField.requestFocus();
}

更新 我认为编辑不支持。我尝试了简单的html代码,如下所示:

<html><head><style>div{background-color: blue}</style></head>   <body>Hello <h1>How are you?</h1> <p>My name is <b> Abhishek</b></p><div>Whats up?</div>

它没有显示内联或外部的任何样式。现在唯一的方法是逐个字符地读取docx文件并检查每个文件的字体并在RTEditor中适当地添加它。

1 个答案:

答案 0 :(得分:0)

富文本编辑器支持readme.md中提到的格式。从html到html的转换只是为了能够保存和加载已编辑的文本,但不是支持每个属性的通用html转换器。该组件可以使用任何格式来保存文本(rtf,markdown,您自己的格式)。如果要从docx文件导入文本,则必须编写自己的导入/导出转换器。如果要导入特定的html格式,可以增强现有的html转换器(例如,支持样式表中的彩色文本)。您可以导入/转换富文本编辑器支持的所有内容,但尝试导入时没有意义,例如表格,因为编辑器不支持这些表格(表格没有跨越的等价物)。