2014-05-02 76 views
0

自動改變我對Android的清單由於其時我轉動我的設備使用android:configChanges="orientation|screenSize",我的設備不挑佈局,土地文件夾中的佈局XML文件中任何一個可以告訴我有什麼問題?配置上改變佈局不是機器人

+0

我在android清單中使用android:configChanges =「orientation | screenSize」 - 這是您的問題。停止使用它。 – Karakuri

+0

在縱向和橫向佈局中使用相同名稱的XML文件。 – prakash

+0

沒有它必須我不能停止使用它 –

回答

2

如果您選擇使用android:configChanges =「orientation | screenSize」,那麼您在屏幕旋轉時手動更新佈局。

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

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
     setContentView(R.layout.portrait); 
    } 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     setContentView(R.layout.landscape); 
    } 
} 

編輯: 使用片段複雜的事情,並採用了android:configChanges = 「方向|屏幕尺寸」,它不建議無妨。你應該在該片段中使用的onSaveInstanceState保存片段的狀態,然後恢復onCreateView片段狀態:

public class DetailsFragment extends Fragment 
{ 
    private EditText ed; 

    public static DetailsFragment newInstance(int index) 
    { 
     return new DetailsFragment(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.fragment_layout, container, false); 
     ed = (EditText) view.findViewById(R.id.fragment_edit); 

     if(null != savedInstanceState) 
     { 
      //restore the text if null != savedInstanceState -> fragment is recreated 
      ed.setText(savedInstanceState.getString("RESTORE_TEXT")); 
     } 

     return view; 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) 
    { 
     if(null != ed) 
     { 
      //save the text written in the edittext 
      outState.putString("RESTORE_TEXT", ed.getText().toString()); 
     } 

     super.onSaveInstanceState(outState); 
    } 
} 

我已經寫在一個項目上面的例子中,你可以找到here,你可以看到更好的如何作品。希望能幫助到你。

+0

,但在片段沒有方法setContentView –

+0

我沒有看到你的問題,你提到你使用片段無處。 –

+0

對不起我的錯誤,但我在這種情況下使用片段我做什麼 –