回答

5

this它描述瞭如何檢測方向改變,那麼在java中改變方向

onCreate()

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

    setContentView(R.layout.activity_main); 
    linearlayout=........; 

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
    // landscape 
    linearlayout.setOrientation(LinearLayout.HORIZONTAL); 
    } 
    else { 
    // portrait 
    linearlayout.setOrientation(LinearLayout.VERTICAL); 
    } 
    .... 
    } 

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

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     // landscape 
     linearlayout.setOrientation(LinearLayout.HORIZONTAL); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
    // portrait 
     linearlayout.setOrientation(LinearLayout.VERTICAL); 
    } 
    } 
+0

非常感謝你Arun =)它工作的很好 – Eydolol

1

在的onCreate()把下面的代碼:

int currentOrientation = getResources().getConfiguration().orientation; 
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { 
    // Landscape 
    linearlayout.setOrientation(LinearLayout.HORIZONTAL); 
} 
else { 
    // Portrait 
    linearlayout.setOrientation(LinearLayout.VERTICAL); 
} 
+0

謝謝vickey! – Eydolol