在对话框或片段对话框中嵌入片段

时间:2012-08-07 19:07:23

标签: android dialog fragment

我正在尝试在Dialog或DialogFragment中嵌入Fragment

public class addAccountDialog extends DialogFragment 
{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.add_account_dialog, container);
    accountType.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) 
            {
            passwordFragment newFragment = new passwordFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_base, newFragment);
            transaction.commit();
            }
        }
    }

}



        <LinearLayout
            android:id="@+id/fragment_base"
            android:layout_width="0px"
            android:layout_height="match_parent"
            android:layout_weight="3" > 
        </LinearLayout>  

我尝试使用Dialog和DialogFragment都没有用。有没有人能够在对话框中实现片段。

提前谢谢你。

罗杰

1 个答案:

答案 0 :(得分:1)

您无法将Fragments置于其他Fragments内。

我的建议是将您的Fragments置于Activity范围内,然后通过XML执行Activity Dialog主题:

<activity android:theme="@android:style/Theme.Dialog" />

编辑:只是一个注释,从API-17(4.2)开始,您现在可以嵌套片段:Nested Fragments