[英]Working with portrait and landscape mode programmatically in android
I want to create the layouts for both portrait and landscape mode (in java) and change the orientations programmatically in java file. 我想为纵向和横向模式(在Java中)创建布局,并以编程方式在Java文件中更改方向。 Can anybody help me?
有谁能够帮助我?
you can programmatically force a change in orientation using the setRequestOrientation()
method of the Activity class. 您可以使用Activity类的
setRequestOrientation()
方法以编程方式强制更改方向。
public class Example extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---change to landscape mode---
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
constant: 要更改为纵向模式,请使用
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
常量:
For more information, refer this . 有关更多信息,请参阅此 。
EDIT 编辑
you can add orientation to your specific Activity
in AndroidManifest.xml
like this: 您可以在
AndroidManifest.xml
向特定的Activity
添加方向,如下所示:
<activity android:name=".DemoActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
As far as Layouts are concerned, it is best advised to create different folders under res/
. 就布局而言,最好建议在
res/
下创建不同的文件夹。 For example, you can create res/layout-port
for portrait and res/layout-land
for landscape. 例如,您可以为纵向创建
res/layout-port
,为风景创建res/layout-land
。
Try this: 尝试这个:
if(context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
// code to do for Landscape Mode
} else {
// code to do for Portrait Mode
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.