dialogFragment 中的视频全屏模式

Video fullscreen mode in dialogFragment

我正在使用此代码从自定义适配器打开片段对话框:

HomeActivity activity = (HomeActivity) (context);
            FragmentManager fm = activity.getSupportFragmentManager();
            editor.putString("post_media", remainder);
            editor.apply();
            PostVideoFragment alertDialog = new PostVideoFragment();
            alertDialog.show(fm, "fragment_alert");

这是片段对话框中的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

这是片段对话框的代码:

public class PostVideoFragment extends DialogFragment {

    VideoView post_video;



    public static String MISDATOS= "MisDatos";

    SharedPreferences prefs;
    SharedPreferences.Editor editor;
    String post_media;


    public static PostVideoFragment newInstance() {
        PostVideoFragment fragment = new PostVideoFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_post_video, container, false);




        prefs = getActivity().getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);

        editor = prefs.edit();


       post_media = prefs.getString("post_media", "no");

        String url = "https://f..postsmedia/";

        final VideoView videoView;
        videoView = (VideoView)v.findViewById(R.id.video);
        videoView.setVideoPath(url+post_media);
        videoView.start();



        return v;
    }
}

功能不错,可以播放视频了,但是横屏的时候需要全屏播放,现在只有非常小的显示了。

这是输出:

人像模式:

横向模式:

我有一个关于对话框大小的类似问题,为我解决的是用 LayoutParams 定义对话框的大小,如下所示:

Window window = yourDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());

//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(lp);
}

但为什么不能将对话从 XML 设置为 match_parent 并以它结束?

似乎默认对话框 LayoutParams (wrap_content,wrap_content) 覆盖了 XML (match_parent,match_parent) 中的 LayoutParams。

这就是为什么您可以使用固定大小 (150dp,150dp) 并看到您的对话框大小发生变化,但是您的对话框将不会响应,因此这不是一个明智的解决方案。

DialogFragmentonStart 中,您可以将高度和宽度设置为 MATCH_PARENT,如下所示:

@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
        //You need to add below lines in order to show FullScreen VideoView without the status bar
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}