我有一个问题,我需要在
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>
我想要的是用我的片段替换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();
解决方法
通常你直接使用
DialogFragment这个名字是自我解释的.
这是我的代码示例,其中int发送为arg.
所以基本上你创建了一个扩展DialogFragment的DialogFragment.
您必须编写newInstance和onCreateDialog方法.
然后在调用片段中创建该片段的新实例.
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; } }
通过执行此操作,从另一个片段调用对话框片段.
请注意,值0是我发送的int.
YourDialogFragment yourDialogFragment = YourDialogFragment.newInstance(0); YourDialogFragment.show(getFragmentManager().beginTransaction(),"DialogFragment");
在您的情况下,如果您不需要传递任何内容,请删除DialogFragment中的相应行,并且不要在YourDialogFragment.newInstance()中传递任何值
编辑/ FOLLOW
不确定真正理解你的问题.
如果您只需要用另一个片段替换片段,则使用
getFragmentManager().beginTransaction().replace(R.id.your_fragment_container,new YourFragment()).commit();