alexanderblom 是正确的,DialogFragment
它作为一个对话框,但是它可以作为一个片段setShowsDialog(false);
最后,以下对我有用:
文件:res/layout/wifidirect_dialog_wifidirect:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/dialog_wifidirect_layout">
<LinearLayout
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical" >
<!-- This is replaced during runtime -->
<RelativeLayout
android:id="@+id/frag_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top" >
</RelativeLayout>
</LinearLayout>
<!-- The Cancel Button -->
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
<Button
android:id="@+id/dialog_wifidirect_cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cancel"/>
</LinearLayout>
文件 src/.../WifiDirectDialog.java:
public class WiFiDirectDialog extends DialogFragment {
public static final String TAG = "WifiDirectDialog";
public static final String DEVICE_LIST_FRAGMENT_TAG = "WIFIDIRECT_DEVICE_LIST_FRAGMENT";
public static WiFiDirectDialog newInstance(){
WiFiDirectDialog wDialog = new WiFiDirectDialog();
//We want this Dialog to be a Fragment in fact,
//otherwise there are problems with showing another fragment, the DeviceListFragment
wDialog.setShowsDialog(false);
//wDialog.setStyle(SherlockDialogFragment.STYLE_NORMAL,android.R.style.Theme_Holo_Light_Dialog);
//We don't want to recreate the instance every time user rotates the phone
wDialog.setRetainInstance(true);
//Don't close the dialog when touched outside
wDialog.setCancelable(false);
return wDialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.v(TAG,"onCreateView");
View view = inflater.inflate(R.layout.wifidirect_dialog_wifidirect,container, false);
//Log.v(TAG,"FragmentTransaction started");
ListFragment listFragment = new YourListFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.addToBackStack(DEVICE_LIST_FRAGMENT_TAG)
.replace(R.id.frag_list,deviceListFragment,DEVICE_LIST_FRAGMENT_TAG)
.commit();
//Log.v(TAG,"FragmentTransaction finished");
return view;
};
@Override
public void onActivityCreated(Bundle savedInstanceState){
Log.v(TAG,"onActivityCreated");
super.onActivityCreated(savedInstanceState);
Dialog dialog = getDialog();
dialog.setTitle(R.string.wifidirect_dialog_title);
// Set button listeners etc...///
Button cancelButton = (Button) view.findViewById(R.id.dialog_wifidirect_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
}