簡體   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