如何重用 setContentView 中设置的布局? Android

How to reuse layout set in setContentView? Android

我正在尝试在我的清单中使用 android:configChanges="keyboardHidden|orientation|screenSize",这样我就不会在配置更改发生时丢失 VideoView 的状态。这意味着我需要自己在配置上选择布局。我遇到的问题是,当我第一次初始化要在 onCreate 中使用的视图时,此处:

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

        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.activity_make_photo_video_land);

        } else if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_make_photo_video);
        }
}

它选择了正确的布局。但是当我需要调整配置更改的布局时,如下所示:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_LANDSCAPE) {
            setContentView(R.layout.activity_make_photo_video_land);
        } else if (getResources().getConfiguration().orientation ==
                Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.activity_make_photo_video);
        }


    }

它没有使用相同的布局实例,而是重新加载它,这让我的 VideoView 变黑了。之后我无法按任何按钮。

有没有办法重用初始布局?我试图将这样的视图 setContentView(R.layout.activity_make_photo_video_land); 设置为 Activity activity 对象,但 IDE 不允许,它说 Activity is incompatible with void。因为如果我可以获得对该视图的引用,那么我就可以重用它。还是有一种我没有看到的更简单的方法?

谢谢。

我为您的问题建议替代和简单的解决方案:

1. Move layout activity_make_photo_video_land to /res/layout-land/ folder and rename it as activity_make_photo_video.xml

This way your activity_make_photo_video_land view (rename as activity_make_photo_video) will be inflated when orientation changes to LANDSCAPE

因此,您不需要通过 java 代码手动处理这些设施。

将 onCreate() 更改为:

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

            setContentView(R.layout.activity_make_photo_video_land);

}

您的应用程序将在 /res/layout//res/layout-land/[=33 中具有单一布局 activity_make_photo_video =] 两个文件夹

更新 :

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

覆盖 onSaveInstanceState(Bundle) 并在此处保存您的状态

您可以查看更多Here

我希望这会奏效

感谢@Kushal 的建议,我研究了 onSaveInstanceState(Bundle savedInstanceState)onRestoreInstanceState(Bundle savedInstanceState) 的两种方法来保存我的 VideoView 配置更改状态(因为这是我问题的根源,并且使用 setContentView 只是一个 hack idea)。原来我在那里找到了一些代码。这是我所做的:

  1. 确保我有横向 xml 和常规 xml。在我的 layout-landlayout 文件夹中分别具有相同的名称。

  2. 像这样覆盖这 2 个方法:

    private int position = 0;
    
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        // use onSaveInstanceState in order to store the video
        // playback position for orientation change
        savedInstanceState.putInt("Position",  videoView.getCurrentPosition());
        videoView.pause();
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // use onRestoreInstanceState in order to play the video playback
        // from the stored position
        position = savedInstanceState.getInt("Position");
        videoView.seekTo(position);
    }
    
  3. 从我的清单中取出 android:configChanges="keyboardHidden|orientation|screenSize"

  4. 删除了 onConfigurationChanged(Configuration newConfig) 方法(不再需要 #3 了)。

做到了!我可以随时改变方向,随时录制我的视频。从重新编码视频返回时,它可以很容易地以横向或纵向显示,而不会丢失视频(屏幕永远不会变黑)。

我现在唯一的问题是我的 OnTouchListener 被禁用了,但那是另一个问题。