我正在尝试生成包含动态生成的文本的弹出窗口。日志输出清楚地表明所需的文本实际上已设置为textView,但我不明白为什么它没有显示在应用程序上。

MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    linearLayout = (LinearLayout) findViewById(R.id.main_activity_parent_layout);
    int id = item.getItemId();
    if(id == R.id.info_button){

        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

        //Updating a textview in different layout xml from the main activity
        LinearLayout vi = (LinearLayout)layoutInflater.inflate(R.layout.activity_info, null);
        TextView textView = vi.findViewById(R.id.info);
        textView.setText(R.string.main_activity_info);
        Log.i("MainActivity",String.valueOf(textView.getText()));

        ViewGroup container = (ViewGroup)layoutInflater.inflate(R.layout.activity_info,null);

        popupWindow = new PopupWindow(container, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //Note showAtLocation method should always in end for proper rendering of poup Window
        popupWindow.showAtLocation(linearLayout,Gravity.TOP,0,100);

    }
    return super.onOptionsItemSelected(item);
}


activity_info.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:padding="8dp"
        android:text="hello"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/black"/>

</LinearLayout>


弹出窗口显示您好,这是硬编码的文本。

最佳答案

您两次夸大R.layout.activity_info而失去了在此过程中更改的TextView。进行以下更改以保持第一次充气和您的修改:

popupWindow = new PopupWindow(vi, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);

关于android - 为什么在硬编码的情况下将文本动态设置为TextView在弹出窗口中不可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45389710/

10-11 20:47
查看更多