[英]Get access to setText to a view that is not in the default XML layout
I want to .setText
to a view that is not in the default XML layout - activity_main.xml
. 我想.setText
的看法,是不是在默认XML布局- activity_main.xml
。
For a better understanding, I have 2 layout's: activity_main.xml
and popup_window.xml
. 为了更好地理解,我有2个布局: activity_main.xml
和popup_window.xml
。
The TextView
is in the popup_window.xml
. TextView
在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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.