【发布时间】:2014-01-21 12:31:21
【问题描述】:
我有一个自定义的DialogFragment,其中包含一个布局、一些 UI 元素和一个 Fragment holder 布局。我需要做的是将内容片段膨胀到支架布局中并在其中提供导航。单击添加的片段内的按钮时,视图将导航到另一个视图。该片段将被同一持有者中的另一个片段替换,即contentFragment1 将显示一些数据,单击预览按钮时,contentFragment1 将替换为contentFragment2。
我在某处读到,您无法将硬编码为xml 的片段替换为另一个片段。
所以我试图将contentFragment1 从对话框片段的onActivityCreated() 添加到viewholder。但是我收到一个错误,即找不到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>
【问题讨论】:
-
也发布 R.layout.reports_dialog 布局
标签: android android-layout android-fragments android-dialogfragment fragmenttransaction