简体   繁体   English

如何在Android中为纵向和横向模式提供单独的布局

[英]how to provide separate layouts for portrait and landscape mode in android

I am working with a Real time app which contains Tabs through out the application. 我正在使用实时应用程序,其中包含整个应用程序中的标签页。 When I try to switch the orientation between portrait and landscape, I have code to show a separate layout for landscape. 当我尝试在纵向和横向之间切换方向时,我有代码显示横向的单独布局。 For this I have used the onConfigurationChanged method as follows 为此,我使用了onConfigurationChanged方法,如下所示

public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
        setContentView(R.layout.eventslist);
    }else if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        setContentView(R.layout.eventstimeline);
    }
}

And also in the manifest file for that particular activity I have used two attributes as follows 在该特定活动的清单文件中,我还使用了以下两个属性

android:screenOrientation="sensor"

android:configChanges="orientation|keyboardHidden"

But when I rotate the device I cannot see the separate layout for the landscape mode which I have specified in onConfigurationChanged . 但是,当我旋转设备时,我看不到在onConfigurationChanged指定的横向模式的单独布局。 Also I don't want to see the tabs in the landscape mode. 另外,我也不想在横向模式下看到这些标签。 Any idea about this. 关于这个的任何想法。

In Resources you can define different layout for each orientation, similar to drawables with screen density. 在参考资料中,您可以为每个方向定义不同的布局,类似于具有屏幕密度的可绘制对象。

Beside layout folder, you can put layouts in: 布局文件夹旁边,您可以将布局放入:

layout-port : layout for portrait orientation layout-port :纵向布局

layout-land : layout for landscape orientation layout-land :横向布局

Ofcourse, you should use same name for those layouts, same as with drawables. 当然,对于这些布局,您应该使用与可绘制对象相同的名称。

Then just setContentView(R.layout. thatlayout ); 这时正好的setContentView(R.layout thatlayout。);

If you don't care that your activity is recreated when you change orientation, just create your layouts xml files for portait and landscape with the same name and put the one for portait in the layout folder and the one for landscape in layout-land folder. 如果您不在乎更改方向时会重新创建活动,只需为portait和landscape创建具有相同名称的布局xml文件,然后将一个portait用于布局,并将其放置在layout-land文件夹中。 。 Then you can do 那你可以做

protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.xml_with_same_name_for_landscape_and_portait);
}

If you don't want to desrtoy and recreate your activity each time the orientation changes, it seems that you are doing well, but the Activity doc says: 如果您不想在每次方向改变时都进行变形和重新创建活动,那么看来您做得不错,但是“ 活动”文档会说:

If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called. 但是,如果配置更改涉及您不处理的任何内容,则该活动仍将重新启动,并且不会调用onConfigurationChanged(Configuration)。

So perhpas your problem is onConfigurationChanged() is never called. 因此,您的问题是onConfigurationChanged()永远不会被调用。 In this case chek yout android:configChanges attribute in the manifest. 在这种情况下,请检查清单中的android:configChanges属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM