Aji*_*ana 5 android android-layout android-fragments android-dialogfragment fragmenttransaction
我有一个自定义DialogFragment,其中包含布局,一些UI元素和Fragment holder布局.我需要做的是将内容片段膨胀到持有者布局中并在其中提供导航.单击添加的片段内的按钮后,视图将导航到另一个视图.片段将被同一个持有者中的另一个片段替换,contentFragment1即将显示一些数据,点击预览按钮将替换contentFragment1为contentFragment2.我读到某处你无法xml用另一个替换硬编码的片段.所以我尝试添加contentFragment1到viewholder从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);
}
Run Code Online (Sandbox Code Playgroud)

<?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>
Run Code Online (Sandbox Code Playgroud)
在尝试了很多之后,我得出了一个onCreateDialog()没有视图的结论,它只是设置了一个关于调用的视图setView().
这就是为什么在dialogfragment的布局中添加动态(framelayout标签)或静态片段(片段标签)不会给出父视图或重复的id错误.
为了实现上述目的,应该使用onCreateView可以动态膨胀的framelayout标签.然后将标题和警报按钮添加到布局中.
只需确保 reports_dialog 布局包含一个 id myFragmentHolder 像这样的布局
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)