我有一个共同的偏好,其中一段正在存储。该值正确写入共享首选项,我可以从中读取并将其显示到 webview 上;没有错误。
我在 XML 文件中使用了一个 sting 声明,并在 justify 模式下对齐它,我成功地显示了数据。
我的问题是当我动态使用字符串时,如何在对齐模式下将上述文本与 web 视图对齐;
问题解释
假设我从用户那里获取了一段数据,或者我在活动中为字符串分配了一个大数据,而不是在 string.xml 文件中,那么应该如何对齐对齐;
下面的代码是我成功地从 string.xml 文件中以 justify 模式显示数据。
public class MainActivity extends Activity {
SharedPreferences pref;
SharedPreferences.Editor edi;
String prefname="Test";
static final String Id ="Desc";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s=getResources().getString(com.testwebviewalign.R.string.MyDesc);
//writing to shared
pref=getSharedPreferences(prefname, MODE_PRIVATE);
edi=pref.edit();
edi.putString(Id,s );
edi.commit();
//reading from shared
pref=getSharedPreferences(new MainActivity().prefname, MODE_PRIVATE);
String D=pref.getString(new MainActivity().Id, null);
WebView wv=(WebView)findViewById(com.testwebviewalign.R.id.webView1);
//wv.setVerticalScrollBarEnabled(true);
wv.loadData(D, "text/html", "utf-8");
}
}
用于字符串声明的 XML 文件
<string name="MyDesc">
<![CDATA[
<html>
<body style="text-align:justify;color:black">
This valuse is for testing the alignment of the android file when using the webview, Actual thing is to convert the local text view activity in to the webview so that we can align text in justify mode. Justify mode that we can align with two sides porerly and text will display in a decen manner and well aligned format. Only with webview its posiible in native android activity. Thak you
</body>
</html>
]]>
</string>
我需要的是如何在对齐模式下显示数据,当它没有从 xml 文件中读取时,假设它是在如下代码中声明和初始化的
public class MainActivity extends Activity {
SharedPreferences pref;
SharedPreferences.Editor edi;
String prefname="Test";
static final String Id ="Desc";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s="This valuse is for testing the alignment of the android file when using the webview, Actual thing is need to convert the local text view activity in to the webview so that we can align text in justify mode. Justify mode is sthat we can align with two sides porerly and text will display in a decen manner and well aligned format. ONly with webview its posiible in native android activity. Thak you";
//writing to shared
pref=getSharedPreferences(prefname, MODE_PRIVATE);
edi=pref.edit();
edi.putString(Id,s );
edi.commit();
//reading from shared
pref=getSharedPreferences(new MainActivity().prefname, MODE_PRIVATE);
String D=pref.getString(new MainActivity().Id, null);
WebView wv=(WebView)findViewById(com.testwebviewalign.R.id.webView1);
//wv.setVerticalScrollBarEnabled(true);
wv.loadData(D, "text/html", "utf-8");
}
}
请帮忙,感谢您的帮助;