繁体   English   中英

方向更改在片段中获得相同的布局

[英]orientation change getting the same layout in fragment

我正在从肖像到风景以及从风景到肖像的方向变化。 我的代码如下

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    //Set titlebar as "my@heaven"
    getSherlockActivity().getSupportActionBar()
    .setBackgroundDrawable(getResources().getDrawable(R.drawable.myheaven));

    view = inflater.inflate(R.layout.memorial_listing, container, false);   

    setUpView(savedInstanceState);

    return view;
}

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {


    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

    }

  }

我也将其添加到清单中

android:configChanges="orientation|keyboardHidden|screenSize"

和相同的布局,我将布局文件夹放置为纵向布局,将布局土地放置为横向布局,但是当我尝试旋转屏幕(如果从纵向开始)时,仍然从布局文件夹获取布局。 屏幕旋转但没有找到正确的文件夹来显示布局。 请帮忙

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

    // Get a layout inflater (inflater from getActivity() or getSupportActivity() works as well)
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(// new orientation is potrait){
        // load potrait layout
    }else{
       // load landscape layout
    }
}

方向更改时,此方法将接收回调。 因此,在接收回调时增加布局。

PS:两个布局应使用不同的名称。 不要将横向布局放在layout-land文件夹中。 当我遇到此问题时,我不得不从布局区域拉出布局,然后将其放在布局文件夹中。 尽管这不是一个好习惯,但是就我而言,它仅以这种方式起作用。

下面对我有用

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

    Log.e("On Config Change","LANDSCAPE Mode");
}else{

    Log.e("On Config Change","PORTRAIT Mode");
}

可以请您检查一下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM