【发布时间】:2015-02-26 21:40:13
【问题描述】:
我创建了一个弹出窗口以在我的应用程序中显示免责声明,但我无法弄清楚为什么我无法将文本设置为 TextView。这是一个 HTML 字符串,所以我这样做了:
private void showPopup(final Activity context) {
final PopupWindow pwindo;
Button btnClosePopup;
try {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
TextView txt = (TextView)findViewById(R.id.txtView);
txt.setText(Html.fromHtml(getString(R.string.tos_text)));
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
popup_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4d4d4d"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:textColor="#ffffff" />
<Button
android:id="@+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close" />
</LinearLayout>
strings.xml
<string name="tos_text"><![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>
仅当我在 XML 文件中添加 android:text="@string/tos_text" 时它才有效,但我看到了原始 html 代码。可能是什么问题?
【问题讨论】:
-
几乎可以肯定是
TextView txt = (TextView) layout.findViewById(R.id.txtView);注意添加layout. -
成功了!非常感谢!
-
@smarthouse 没问题 :-)
-
你能告诉我在这种情况下如何添加滚动条吗?我试图在这里搜索,但我什么也没找到......
标签: android html popup textview window