对话框内的android片段

El *_*uli 9 android android-fragments

我有一个问题,我需要fragment在一个android.app.Dialog

这是xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/marchecharts"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想要的是marchecharts用我的片段替换,任何人都可以帮忙

谢谢

Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.marche_charts_parent);


//this is the part I think I need
Fragment fragment = new MarcheChartsFragment();
FragmentTransaction ft = ((FragmentActivity) dialog.getOwnerActivity()).getFragmentManager().beginTransaction();
ft.replace(R.id.marchecharts, fragment);  
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();

dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
dialog.show();
Run Code Online (Sandbox Code Playgroud)

HpT*_*erm 12

通常你直接使用DialogFragment,这个名称是自我解释的.

这是我的代码示例,其中intsend为arg.

所以基本上你创建了一个DialogFragment扩展DialogFragment.你必须写newInstanceonCreateDialog方法.然后在调用片段中创建该片段的新实例.

public class YourDialogFragment extends DialogFragment {
    public static YourDialogFragment newInstance(int myIndex) {
        YourDialogFragment yourDialogFragment = new YourDialogFragment();

        //example of passing args
        Bundle args = new Bundle();
        args.putInt("anIntToSend", myIndex);
        yourDialogFragment.setArguments(args);

        return yourDialogFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //read the int from args
        int myInteger = getArguments().getInt("anIntToSend");

        View view = inflater.inflate(R.layout.your_layout, null);

        //here read the different parts of your layout i.e :
        //tv = (TextView) view.findViewById(R.id.yourTextView);
        //tv.setText("some text")

        return view;
    }
}
Run Code Online (Sandbox Code Playgroud)

通过执行此操作,从另一个片段调用对话框片段.请注意,该值0是我发送的int.

YourDialogFragment yourDialogFragment = YourDialogFragment.newInstance(0);
YourDialogFragment.show(getFragmentManager().beginTransaction(), "DialogFragment");
Run Code Online (Sandbox Code Playgroud)

在您的情况下,如果您不需要传递任何内容,请删除DialogFragment中的相应行,并且不要传递任何值 YourDialogFragment.newInstance()

编辑/ FOLLOW

不确定真正理解你的问题.如果您只需要使用另一个片段替换片段

getFragmentManager().beginTransaction().replace(R.id.your_fragment_container, new YourFragment()).commit();
Run Code Online (Sandbox Code Playgroud)

  • 再想一想,我认为这不能解决问题,因为我手上已经有一个片段,我只需要在对话框中显示它 (2认同)