2013-11-26 43 views
6

我有,我需要顯示在android.app.Dialog內部對話

這裏fragment一個問題,Android的碎片是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(); 

回答

7

烏蘇盟友你直接用DialogFragment這個名字是自我解釋的。

這裏是我的代碼示例,int作爲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; 
    } 
} 

通過這樣做調用對話框片段是從另一個片段完成的。 請注意,值0是我發送的整數。

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(); 
+0

感謝您的迴應! 我在onCreateDialog中出現錯誤:類型不匹配:無法從視圖轉換爲對話框在「返回視圖」行 –

+0

第二想法,我不認爲這回答了問題,因爲我已經有一個片段在手中,我只需要顯示它在對話框 –

+0

據我瞭解你的問題。你想在對話框中顯示你自己的片段。如果這是你的問題,我提供的代碼說明了這一點。 – HpTerm