【问题标题】:I have a linear layout and a frame layout , i inflate my fragment in the frame layout , How to access the linear layout from the fragment我有一个线性布局和一个框架布局,我在框架布局中为我的片段充气,如何从片段访问线性布局
【发布时间】:2017-01-11 21:03:28
【问题描述】:
<RelativeLayout
...>
<LinearLayout
...>
<TextView
.../>
</LinearLayout>
<FrameLayout
.../>
</RelativeLayout>

在我的主要活动中:

我在框架布局中替换了我的片段,如何从我的片段类访问线性布局。

错误:

只能在实际创建视图的位置访问视图。

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: android android-layout android-fragments layout


【解决方案1】:

您可以使用

Fragment 访问 Activity 中的任何布局

Layout yourLayout = (Layout)getActivity().findViewById(R.id.idOfLayout);

片段可以将 Activity 实例与getActivity() 一起使用,例如在 Activity 布局中查找视图

【讨论】:

  • 我不这样做 - setcontentview(R.id.layout) 在我的活动中,我只是将我的片段替换为框架布局。那么我可以在我的片段中执行此操作吗?
  • 我可以使用 LinearLayout ll = (LinearLayout)getActivity().findViewById(R.id.idoflayout);在我的片段中
  • 如果这个答案对您有帮助,请投票并接受我的回答。这就是 SO 的工作原理:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多