首页 > 解决方案 > 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;
    }
}

功能是正确的,可以播放视频,但是当屏幕处于横向模式时我需要全宽播放它,现在它只以非常小的模式显示。

这是输出:

人像模式:

在此处输入图像描述

横向模式:

在此处输入图像描述

标签: androidandroid-dialogfragment

解决方案


onStart您的DialogFragment中,您可以将高度和宽度设置为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);
    }
}

推荐阅读