android片段内部对话框
我遇到一个问题,需要在android片段内部对话框,android,android-fragments,Android,Android Fragments,我遇到一个问题,需要在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" > <FrameLa
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>
通常你直接使用自己解释的名字
下面是一个带有int
sendaasarg的代码示例
因此,基本上您创建了一个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()中的任何值
编辑/跟踪
不确定是否真正理解了你的问题。
如果您只是需要用另一个片段替换一个片段,您可以使用
getFragmentManager().beginTransaction().replace(R.id.your_fragment_container, new YourFragment()).commit();
谢谢你的回复!我在onCreateDialog中出错:类型不匹配:无法在“返回视图”行中从视图转换到对话框经过再三考虑,我认为这并不能解决问题,因为我手中已经有一个片段,我只需要在对话框中显示它,就我理解您的问题而言。您希望在对话框中显示自己的片段。如果这是你的问题,我提供的代码就是这样的。谢谢你,你基本上给了我一个我需要的提示,我有一个片段,我需要在手机上显示为一个普通片段,但在平板电脑上显示为一个对话框,所以我最终从DialogFragment扩展了我自己的片段,到目前为止效果很好
getFragmentManager().beginTransaction().replace(R.id.your_fragment_container, new YourFragment()).commit();