将片段布局纵向更改为横向 [英] Change fragment layout portrait to landscape
问题描述
我使用片段,但我的问题是:当我更改方向时,如果我的scrollView中有2列,即使它们是纵向的,它们仍然在屏幕上;或者如果我在纵向模式下只有1列,则在传递到风景时我只有1列.
I work with fragment but my problem is that : When I change orientation, if I had 2 columns in my scrollView they still in the screen even it is portrait, or If I had one column in portrait mode when I pass to landscape I had only one column.
我有3个具有相同名称的布局,只有可更改的列数,我把它们全部放在文件夹布局,布局土地和布局端口中.
I had 3 layout with the same name only number of columns which change, I put it all in folder layout, layout-land and layout-port.
我需要的是,当我从肖像传递到风景时,我将从2列传递到1列,反之亦然.
推荐答案
您不能仅仅将其全部放在文件夹布局,layout-land和layout-port中以使其起作用.您还需要执行3个步骤:1.在values文件夹中创建一个layout.xml文件2.将具有2列的xml布局放在layout-land中,将具有1列的布局放在layout-port中3.在片段的onCreateView()中,在layout.xml文件而不是当前布局中增加布局名称.
You can't just put it all in folder layout, layout-land and layout-port to make it work. There are 3 more steps you need to do: 1. Create a layout.xml file in values folder 2. Put xml layout with 2 column in layout-land and layout with 1 column in layout-port 3. In onCreateView() in your fragment, inflate the layout's name in layout.xml file instead current layout.
有关更多详细信息,我将向您展示一些代码:您将要编写一个MainFragment,并为其布局创建一个fragment_main.xml.您需要做的是:
For more details, I'll show you some code: You're gonna write a MainFragment and you create a fragment_main.xml for its layout. What you need to do is:
-
在layout文件夹(1列布局)中写入fragment_main.xml
Write fragment_main.xml in layout folder (1 column layout)
在layout文件夹(2列布局)中写入fragment_main_land.xml
Write fragment_main_land.xml in layout folder (2 column layout)
使用代码行在values-land文件夹中创建layouts.xml文件<资源>< item name ="layout_main" type ="layout"> @ layout/fragment_main</item></resources>
Create layouts.xml file in values-land folder with the code lines
<resources>
<item name="layout_main" type="layout">@layout/fragment_main</item>
</resources>
使用代码行在values-land文件夹中创建layouts.xml文件
Create layouts.xml file in values-land folder with the code lines
< resources>< item name ="layout_main" type ="layout"> @ layout/fragment_main_land</item></resources>
在MainFragment的onCreateView方法上,inflater.inflate(R.layout.layout_main,null);
on onCreateView method of MainFragment, inflater.inflate(R.layout.layout_main, null);
这篇关于将片段布局纵向更改为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!