屏幕方向调节---LANDSCAPE与PORTRAIT
对屏幕方向调节只需要通过Activity.getRequestOrientation()方法和Activity.setRequestOrientation()方法即可。
实例说明:两个按钮,可以横向和纵向调节屏幕。
MainActivity.java代码:
public class MainActivity extends Activity {
private Button button1,button2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED){
button1.setEnabled(true);
button2.setEnabled(true);
}
else if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
button1.setEnabled(true);
button2.setEnabled(false);
}
else if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
button1.setEnabled(false);
button2.setEnabled(true);
}
}
}
其中,if语句的作用是当屏幕方向是默认时,可以随意调节,当屏幕纵向时,只能调节成横向,反之亦然。