繁体   English   中英

获取对setText的访问权,该视图不在默认XML布局中

[英]Get access to setText to a view that is not in the default XML layout

我想.setText的看法,是不是在默认XML布局- activity_main.xml
为了更好地理解,我有2个布局: activity_main.xmlpopup_window.xml
TextViewpopup_window.xml

使用以下代码:

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

或者,如果您想使用该布局作为对话框的内容视图(我假设),请按以下方式添加它:

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

<<<<<编辑:>>>>>

好的,如果我从您的评论中了解到,该布局是预定义对话框的一部分。 然后简单地:

TextView tv = (TextView) dialog.getContentView().findViewById(R.id.my_text_view);
tv.setText("My text here");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM