无法在PopupWindow中将文本设置为TextView

时间:2015-02-26 21:40:14

标签: android html popup textview window

我创建了一个弹出窗口,在我的应用程序中显示免责声明,但我无法弄清楚为什么我无法将文本设置为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代码。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

尝试下一步:

    private void showPopup() {
            //if you call this method correctly then you do not need to wrap 
            // this method by try-catch block which affects performance

            LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);

            final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

            //get txt view from "layout" which will be added into popup window
            //before it you tried to find view in activity container
            TextView txt = (TextView) layout.findViewById(R.id.txtView);
            txt.setText(Html.fromHtml(getString(R.string.tos_text)));

            //init your button
            Button btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    pwindo.dismiss();
                }
            });

            //show popup window after you have done initialization of views
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
    }

修改

要添加可滚动内容,您需要更改弹出窗口初始化的方法:

final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

您需要将窗口宽度更改为ViewGroup.LayoutParams.MATCH_PARENT。

然后你需要用ScrollView包装你的文本视图,它包含root child - LinearLayout:

popup_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/popup_element"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#4d4d4d"
          android:orientation="vertical"
          android:padding="10sp">

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <TextView
                android:id="@+id/txtView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5sp"
                android:textColor="#ffffff"/>
    </LinearLayout>
</ScrollView>

<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>

ScrollView包含权重字段: 机器人:layout_weight = “1” 我添加了这个以避免在文本填充所有可见内容时隐藏按钮。

编辑2:

如果您不想填满所有屏幕,也可以修复滚动视图的高度:

-----

 <ScrollView
        android:layout_width="match_parent"
        android:layout_height="150dp">

-----