我是碎片新手。我想在Dialog中显示图像。任何人都可以提供如何显示支持所有设备的DialogFragment的代码片段吗?
答案 0 :(得分:8)
制作一个布局文件并将图像放入其中(例如dialog_fragment.xml)
制作对话框片段(例如MyDialogFragment.java)
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
public class MyDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_fragment, container);
// This shows the title, replace My Dialog Title. You can use strings too.
getDialog().setTitle("My Dialog Title");
// If you want no title, use this code
// getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
导入:import android.support.v4.app.FragmentManager;
FragmentManager fm = getSupportFragmentManager();
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(fm, "dialog_fragment");