
正文
Android横、竖屏幕动态切换(layout-land 和layout-port)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
下面是一个例子程序:
1.首先通过以下语句设置Activity为无标题和全屏模式:
// 设置为无标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
2.下面给出xml文件配置,这里我们在res目录下建立layout-land和layout-port目录,相应的layout文件不变,比如main.xml。layout-land是横屏的layout,layout-port是竖屏的layout,其他的不用管模拟器自动寻找
main.xml文件如下:
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/blue"
android:text="the portrait"
/>
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_button1" />
</LinearLayout>
这个xml文件需要在上述所说的2个文件夹下都需要放置.
3.获取资源id的view:
mButton01 = (Button) findViewById(R.id.myButton1);
mTextView01 = (TextView) findViewById(R.id.myTextView1);
4.返回当前显示Activity的显示状态(横屏还是竖屏)






