项目中遇到的横竖屏切换

来源:互联网 发布:写歌词软件官方网站 编辑:程序博客网 时间:2024/05/21 10:33
 

在我的项目中遇到了横竖屏切换,本来我认为只要在项目res下面写layout-land-800*480 layout-port-800*480  layout-land-480*320 layout-port-480*320 一开始我认为只要我写了这些,屏幕会自动适配的,但是出现的问题是,我要是屏幕适配的话,必须得重新加载,也就是说,光这样写不能做到屏幕适配。

从网上看了一些博客,关于屏幕适配的,说是要重新加载界面,和重新加载数据的。我也看得有点儿糊涂。

重新载入问题。如果不需要从新载入,可以在androidmanifest.xml中加入配置 android:configChanges="orientation",配置android:configChanges的作用就是如文档所说的:Specify one or more configuration changes that the activity will handle itself. If not specified, the activity will be restarted if any of these configuration changes happen in the system。这样在程序中. Activity就不会重复的调用onCreate()甚至不会调用onPause.onResume.只会调用一个 onConfigurationChanged(Configuration newConfig)。
首界面对应的activity我在清单文件中是这样定义的

<activity android:name="LoginActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">

这样定义之后当横竖屏的时候它不走onCreate方法了,它走的是onConfigurationChanged方法,所以我们的横竖屏操作必须写在这个方法中

我是这样写的 现在贴上来

@Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  Log.i("111", "onConfigurationChanged");
  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
   setContentView(R.layout.login);
   save();
  } else {
   setContentView(R.layout.login);
   save();
  }
 }

至于这个save()方法,我是写进去了所有的onCreate方法中的数据。。

王攀一开始给我发了他的代码,如何保存之前的数据,但是他的都是一些常量,就是那个意思

有人还会问save方法不会写 那我就把我的贴上来 代码是

public void save() {
  Util.activitylist.add(this);
  dbService = new DbService(this);
  ll = (LinearLayout) this.findViewById(R.id.linearlayout);
  // 获取系统日志 这个的功能其实很强大的 现在我没必要这样做 会增加JVM的负担 嘿嘿
  // Intent intent=new Intent(LoginActivity.this,LogService.class);
  // startService(intent);

  // 获取手机序列串号 也就是手机的唯一标示
  TelephonyManager tm = (TelephonyManager) LoginActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
  getesnformmobile = tm.getDeviceId();
  edittext_userId = (EditText) this.findViewById(R.id.login_username);
  edittext_password = (EditText) this.findViewById(R.id.login_password);

  login_sure_sure = (Button) this.findViewById(R.id.login_sureMe);
  login_cancle = (Button) this.findViewById(R.id.login_cancle);
  login_changepifu = (Button) this.findViewById(R.id.login_changepifu);
  login_sure_sure.setOnClickListener(this);
  login_cancle.setOnClickListener(this);
  login_changepifu.setOnClickListener(this);
  ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
  networkinfo = manager.getActiveNetworkInfo();
  // 用户名和密码的回显
  SharedPreferences usernamesp = getSharedPreferences("usernamepassword", Context.MODE_PRIVATE);
  String putusername = usernamesp.getString("username", null);
  String putpassword = usernamesp.getString("password", null);
  if (putusername == null) {
   Log.i("Hall", putusername + "此时获取的putusername是空的");
   // 如果我这样做的话应用程序会退出的
   // finish();
  } else {
   edittext_userId.setText(putusername);
   edittext_password.setText(putpassword);
  }
  SharedPreferences putnet = getSharedPreferences("net", Context.MODE_PRIVATE);
  final String ip = putnet.getString("ip", null);
  final String port = putnet.getString("port", null);
  posid = putnet.getString("posid", null);
  postype = putnet.getString("postype", null);

}

这样就完成了横竖屏切换,并且数据重新加载的问题

 

现在还有一个重要的问题要注意

如果我们的清单文件中,像这样定义的

<activity android:name="MenuTypeActivity" />

它横竖屏的时候走的是onCreate方法

只要在onCreate方法中这样写就可以了

 if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
     setContentView(R.layout.responsible);
          Log.i("222", "landscape");
          }
          else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
           setContentView(R.layout.responsible);
           Log.i("222", "portrait");
          }

这样就大功告成了