这里和大家讨论和学习横竖屏。代码在github中,请有兴趣的自行下载。
1. 简单使用:
res目录下面建立layout-land目录,来兼容横屏。横屏会利用layout-land中的layout.xml , 如果找不到,则会利用layout中。所以可以方便的在原有的基础上添加。
如图,activity_main.xml为MainActivity的layout.xml , 横屏时,layout-land中无对应的activity_main.xml,则会利用layout中的。 但是layout-land中有listview_item.xml,则横屏时会利用此xml。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#00f"
android:gravity="right"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/bottomfragment"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>
</LinearLayout>
<ListView
android:layout_below="@id/tv"
android:layout_above="@id/bottomfragment"
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
</ListView>
</RelativeLayout>
如图:
竖屏:
横屏:
2. Fragment使用:
大家经常会用到fragment,fragment加载进来,fragment会根据横屏和竖屏自动选择layout.xml。效果见上图。
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.w("LANDP", "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.listview);
/** listview 的横竖屏 */
String[] from = { "title", "subtitle" };
int[] to = { R.id.title, R.id.subtitle };
List<HashMap<String, String>> list = this.makeDefaultContents();
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.listview_item, from, to);
listView.setAdapter(adapter);
/** fragment的横竖屏 */
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
BottomFragment fragment = new BottomFragment();
fragmentTransaction.add(R.id.bottomfragment, fragment);
fragmentTransaction.commit();
}
3.生命周期讨论:
在androidmanifest.xml中如下:
<!-- 必须有screensize -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
添加android:configChanges="orientation|keyboardHidden|screenSize"。android4.0之后,需要添加screensize,否则,下边的onConfigurationChanged会不起作用,横竖屏切换时还是会销毁activity。
MainActivity中需要添加:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.w("LANDP", "onConfigurationChanged()");
}
这样,系统在横竖屏切换时,不会对Activity和fragment进行销毁,节省了资源。
如果进行销毁,会走一边oncreate和ondestroy,很彻底,很粗暴。哈哈。不过可以学习一下fragment和activity的生命周期。app中建议不要这样。
4.Github 地址:
https://github.com/LiangChaoPossible/ProtraintAndLandscapeDemo