《Android改变屏幕显示方向》由会员分享,可在线阅读,更多相关《Android改变屏幕显示方向(6页珍藏版)》请在金锄头文库上搜索。
1、AVDSDKActivity_main.xmlMainactivity.javapackage mars.androidtest;import android.os.Bundle;import android.app.Activity;import android.content.pm.ActivityInfo;import android.content.res.Configuration;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import andr
2、oid.widget.Button;import android.widget.ImageButton;import android.widget.TextView;public class MainActivity extends Activity private Button button=null;private ImageButton imageButton=null;private TextView textView=null;Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(saved
3、InstanceState);setContentView(R.layout.activity_main);textView=(TextView)findViewById(R.id.myTextViewId);button=(Button)findViewById(R.id.changeButtonId);button.setOnClickListener(new buttonOnClickListener();imageButton=(ImageButton)findViewById(R.id.imageButtonId);private class buttonOnClickListene
4、r implements OnClickListenerOverridepublic void onClick(View arg0) / TODO Auto-generated method stubif(getRequestedOrientation()=ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)textView.setText(错误:无法改变屏幕方向!);elseif(getRequestedOrientation()=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)setRequestedOrientati
5、on(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);else if(getRequestedOrientation()=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);/* (non-Javadoc)* see android.app.Activity#onConfigurationChanged(android.content.res.Configuration)*/Overridepub
6、lic void onConfigurationChanged(Configuration newConfig) / TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);if(newConfig.orientation=Configuration.ORIENTATION_LANDSCAPE)textView.setText(改变屏幕为竖屏显示(当前为横屏显示);imageButton.setImageResource(R.drawable.mldn_portrait);else if(newConfig.
7、orientation=Configuration.ORIENTATION_PORTRAIT)textView.setText(改变屏幕为横屏显示(当前为竖屏显示);imageButton.setImageResource(R.drawable.mldn_landscape);Overridepublic boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;Manifest.xml 注:图片需要找两张