【问题标题】:setText() not working on inflated textviewsetText() 不适用于膨胀的文本视图
【发布时间】:2017-10-23 21:56:19
【问题描述】:


我有一个文本视图,但它不在由setContentView() 方法设置的活动主布局xml 中,但它位于其他xml 文件中,所以我使用了充气器,但它不起作用!
这是我的代码:

LayoutInflater inflater = 
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.anotherXMLfile, null);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText("something");

请帮帮我!

【问题讨论】:

  • 显示整个班级代码。编辑您的问题!

标签: android android-inflate


【解决方案1】:

您还没有设置视图。喜欢

setView(视图);

setContentView(视图);

【讨论】:

  • setContentView(view) 在 setView 无法识别时给出错误。也许还有其他方法可以设置充气视图?
  • 你必须写成:yourContext.setContentView(view);
  • 好的,我已经完成了,它成功了,非常感谢!!
【解决方案2】:

TextView 中的settext 和activity 中的setContentView

在主布局中添加 xml 文件,例如:

<include layout="@layout/your_xml_file_name" />

你可以直接调用 TextView id 并使用它。

setContentView(R.layout.activity_main); 要么 TextView textview=(TextView)findViewById(R.id.textview); textView.setText("写你的内容");

【讨论】:

  • 我在 onCreate 方法中设置了 setContentView。我必须设置充气机视图吗?我该用什么方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多