我有一个自定义DialogFragment
,其中包含一个布局、一些 UI 元素和一个 Fragment holder 布局。我需要做的是将内容片段膨胀到支架布局中并在其中提供导航。单击添加的片段内的按钮时,视图将导航到另一个视图。该片段将被同一持有者中的另一个片段替换,contentFragment1
即将显示一些数据,并在单击预览按钮时将替换contentFragment1
为contentFragment2
。我在某处读到,您无法将硬编码的片段替换为xml
另一个片段。所以我试图添加contentFragment1
到对话框片段的viewholder
from the中。onActivityCreated()
但是我收到一个错误,即R.id.fragmentHolder
找不到指向的资源。可能的原因是什么?
这是我的代码DialogFragment
:
public class MyDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog customDialog = new Dialog(getActivity());
customDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.reports_dialog);
return customDialog;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
android.app.FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.myFragmentHolder, new ReportsListFragment());
fragmentTransaction.commit();
super.onActivityCreated(savedInstanceState);
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/transparent" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="This is my header text view for the Dialog"
android:textSize="18sp" />
<RelativeLayout
android:id="@+id/myFragmentHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/headerlayout" >
</RelativeLayout>