Android新手求助:如何移除新闻详情页的红蓝下划线文本
Hey there! Great job getting your RSS feed list and detail navigation up and running as a new Android dev. Let's tackle that issue where you need to remove all red and blue underlined text from your detail page. These underlines typically come from HTML tags (like <u>) or inline styles in the news content you're displaying. Here are solutions for the two most common ways to show news details:
Scenario 1: Using a TextView for HTML Content
If you're loading RSS content as HTML into a TextView, we can clean up the HTML first to strip out underline-related tags and styles before displaying it. We'll use Jsoup (a handy HTML parsing library) to make this easy.
Step 1: Add Jsoup Dependency
First, add this to your module-level build.gradle file to include Jsoup:
implementation 'org.jsoup:jsoup:1.17.2'
Step 2: Create an HTML Cleaner Utility
Make a helper class to filter out underlines:
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class HtmlUnderlineRemover { public static String stripUnderlines(String rawHtml) { if (rawHtml == null || rawHtml.isEmpty()) return ""; Document doc = Jsoup.parse(rawHtml); // Remove all <u> tags (keep the text inside, just ditch the underline tag) Elements underlineTags = doc.select("u"); for (Element tag : underlineTags) { tag.unwrap(); } // Strip underline styles from elements (e.g., style="text-decoration:underline;color:red") Elements styledElements = doc.select("[style*=text-decoration:underline]"); for (Element element : styledElements) { String currentStyle = element.attr("style"); // Remove the underline part of the style String cleanedStyle = currentStyle.replaceAll("text-decoration\\s*:\\s*underline\\s*;?", ""); if (cleanedStyle.isEmpty()) { element.removeAttr("style"); } else { element.attr("style", cleanedStyle); } } // Optional: If your links (<a> tags) have underlines and you don't need the link functionality // Unwrap the <a> tags to keep just the text Elements linkTags = doc.select("a"); for (Element link : linkTags) { link.unwrap(); } return doc.html(); } }
Step 3: Use the Cleaner in Your Detail Activity
When loading the content into your TextView:
import android.os.Build; import android.text.Html; import android.widget.TextView; // Inside your DetailActivity's onCreate method TextView newsContent = findViewById(R.id.tv_news_detail_content); String rawNewsHtml = getIntent().getStringExtra("NEWS_HTML_CONTENT"); // Adjust this to match your intent key String cleanedHtml = HtmlUnderlineRemover.stripUnderlines(rawNewsHtml); // Load the cleaned HTML into the TextView if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { newsContent.setText(Html.fromHtml(cleanedHtml, Html.FROM_HTML_MODE_COMPACT)); } else { newsContent.setText(Html.fromHtml(cleanedHtml)); }
Scenario 2: Using WebView for News Details
If you're using a WebView to display the content, we can inject custom CSS to globally remove underlines and override those red/blue colors.
Code Example for WebView
import android.webkit.WebView; // Inside your DetailActivity's onCreate method WebView newsWebView = findViewById(R.id.webview_news_detail); newsWebView.getSettings().setJavaScriptEnabled(true); String rawNewsHtml = getIntent().getStringExtra("NEWS_HTML_CONTENT"); // Wrap the raw HTML with custom CSS to remove underlines String styledHtml = "<html><head>" + "<style type='text/css'>" + "/* Remove underlines from all elements */" + "* { text-decoration: none !important; }" + "/* Override any red/blue text colors to match your app's theme */" + "u, a, span { color: #333 !important; } /* Adjust color to your preference */" + "</style></head><body>" + rawNewsHtml + "</body></html>"; newsWebView.loadDataWithBaseURL(null, styledHtml, "text/html", "UTF-8", null);
Quick Alternative for TextView (No HTML Formatting)
If you don't need to keep any HTML formatting at all, you can just extract plain text from the HTML:
String plainText = Jsoup.parse(rawNewsHtml).text(); newsContent.setText(plainText);
This will get rid of all underlines automatically, but you'll lose things like line breaks or bold text.
内容的提问来源于stack exchange,提问作者binod lamichhane

