Lon*_*r13 4 android android-layout android-fragments android-listfragment
我需要从 XML 文件中与 TextView 位于同一平面上的 ListFragment 更新文本视图。但两次尝试都只返回 null:
@Override
public void onCreate(Bundle ice)
{
super.onCreate(ice);
...
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.entry_row, null, from, to, 0);
setListAdapter(mAdapter);
mTextView = (TextView) getActivity().findViewById(R.id.tv); // try #1
mPercentView = (TextView) ((ViewGroup)getView().getParent()).findViewById(R.id.tv); //try #2
// (not tested at the same time)
}
Run Code Online (Sandbox Code Playgroud)
这是 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/info_log_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="@dimen/object_padding"
android:textSize="24sp"
android:textIsSelectable="true" />
<fragment
android:id="@+id/view_log_fragment"
android:name="fragment name..."
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/info_log_view" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
谢谢!
这解决了我的问题,希望它也能帮助您:因此覆盖 onActivityCreated 类并在其中编写以下代码:
View myActivityView=(RelativeLayout)getActivity().findViewById(R.id.parentContainer);
Run Code Online (Sandbox Code Playgroud)
请注意,RelativeLayout 是我包含 Textview 的主要布局。当你在你的片段中有这个时,你可以访问你的片段父视图,现在只需要通过下面的代码获取文本视图:
TextView myTextView=(TextView)myActivityView.findViewById(R.id.myTextView);
Run Code Online (Sandbox Code Playgroud)
希望这有帮助。
归档时间: |
|
查看次数: |
15334 次 |
最近记录: |