调整布局使用JAVA中的设备方向而不是XML

Eyd*_*lol 0 android android-layout device-orientation android-orientation

我想知道如何根据JAVA中的设备Orientation改变我的LinearLayout方向,我发现如何通过XML方式进行布局和布局 - 但是我没有找到如何通过java方式做到这一点.

非常感谢你.

Aru*_*n C 6

说明了如何检测方向改变,那么在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); 
     }
     ....
    }
Run Code Online (Sandbox Code Playgroud)

并在 onConfigurationChanged()

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);
    }
  }
Run Code Online (Sandbox Code Playgroud)