【问题标题】:Get access to setText to a view that is not in the default XML layout获取对不在默认 XML 布局中的视图的 setText 的访问权限
【发布时间】:2013-12-17 04:03:42
【问题描述】:

我想.setText 到一个不在默认 XML 布局中的视图 - activity_main.xml
为了更好地理解,我有 2 个布局:activity_main.xmlpopup_window.xml
TextViewpopup_window.xml 中。

【问题讨论】:

  • 如果popup_window.xml 不是您的活动布局,您将如何显示?
  • @Ran With a PopupWindow...
  • 请检查我的答案

标签: java android layout textview settext


【解决方案1】:

使用以下代码:

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

【讨论】:

    【解决方案2】:
    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");
    

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 2017-10-08
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多