Android 横屏竖屏的学习和讨论

本文探讨了Android应用开发中横竖屏适配的实现方式,包括使用layout-land目录创建横屏布局、Fragment自动选择layout.xml以及生命周期讨论。通过添加android:configChanges属性确保横竖屏切换时Activity和fragment不被销毁,节约资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里和大家讨论和学习横竖屏。代码在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值