make a fullScreen in only layout-land android when play videoView

前端 未结 2 1942
再見小時候
再見小時候 2021-01-14 07:04

I\'m creating android app to play liveStream, I added the videoView in my layout.xml and added the folder of layout-land

I want to make the app shows video full scre

相关标签:
2条回答
  • 2021-01-14 07:30

    You should add your videoview (or content Parent) as last element in your layout file. and use the next code:

    private RelativeLayout.LayoutParams paramsNotFullscreen; //if you're using RelativeLatout           
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
    
    
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) //To fullscreen
        {
            paramsNotFullscreen=(RelativeLayout.LayoutParams)mVideoView.getLayoutParams();
            RelativeLayout.LayoutParams params=new LayoutParams(paramsNotFullscreen);
            params.setMargins(0, 0, 0, 0);
            params.height=ViewGroup.LayoutParams.MATCH_PARENT;
            params.width=ViewGroup.LayoutParams.MATCH_PARENT;
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            mVideoView.setLayoutParams(params);
    
        } 
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
        {
            mVideoView.setLayoutParams(paramsNotFullscreen);
        }
    }
    

    It gets a copy of videoview layoutparams and saves it in a global variable. then creates a new layoutparams object with the previous values but setting now the limits to match_parent and sets it in your videoview. You videoview now is in fullscreen. When you put your device in portrait, paramsNotFullscreen restores previous values.

    UPDATE:

    In your manifest file you must add into Activity declaration the following code to avoid the activity restart:

    android:configChanges="screenLayout|screenSize|orientation"
    
    0 讨论(0)
  • 2021-01-14 07:39

    try this...

    @Override
        public void onConfigurationChanged(Configuration newConfig) 
        {
            super.onConfigurationChanged(newConfig);
    
    
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
            {
                setContentView(R.layout.create_job_l);
    
            } 
            else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
            {
                setContentView(R.layout.create_job_p);
    
            }
        }
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
    
            super.onCreate(savedInstanceState);
            if (getResources().getConfiguration().orientation == 1)
            {
                setContentView(R.layout.create_job_p);
            }
            if (getResources().getConfiguration().orientation == 2)
            {
                setContentView(R.layout.create_job_l);
            }
    
    
        }
    
    0 讨论(0)
提交回复
热议问题