2014-09-04 73 views
0

我是新的android,我已经创建了简单的应用程序,它有3个选项卡,我使用片段活动构建它们。如何访问碎片中的视图?

在活动中我们可以使用findViewById方法。但片段没有这个方法。我怎样才能访问我的观点?

回答

0

您可以将片段类中调用的方法getAcivity()调用背景下您的activity是在它之后,你可以调用findViewById()界的;

样本:

public void onViewCreated() 
    TextView txSample = (TextView)getView().findViewById(R.id.yourid); 
+0

感谢所有的功能操作。这很有用。 – 2014-09-04 00:19:42

+0

我做到了,非常感谢你:) – 2014-09-04 00:25:29

+0

不客气 – 2014-09-04 00:26:58

0
TextView tv; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.fragment_simple_lights, container, false); 
    tv = (TextView) rootView.findViewById(R.id.yourid); 
    return load; 
} 

你最好做onCreateView()中的所有视图相关的操作和onActivityCreated()