I want to .setText to a view that is not in the default XML layout - activity_main.xml.
For a better understanding, I have 2 layout's: activity_main.xml and popup_window.xml.
The TextView is in the popup_window.xml.

有帮助吗?

解决方案

use following code:

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.popup_window, null);
    TextView tv = (TextView)view.findViewById(R.id.yourTextID);
    tv.setText()

其他提示

LinearLayout ll = getLayoutInflater().inflate( R.layout.popup_window, null )
TextView tv = (TextView)ll.findViewById(R.id.my_text_view);
tv.setText("Your text here");
customView.addView(ll);

Or if you want to use that layout as content view for a dialog ( i assume) then add it this way:

dialog.setView(ll);
dialog.show();  

<<<<< EDIT: >>>>>

Ok so if I understood from your comment, that layout is part of a predefined dialog. then simply:

TextView tv = (TextView) dialog.getContentView().findViewById(R.id.my_text_view);
tv.setText("My text here");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top