Android layout 如何在onConfigurationChanged方法中以编程方式更新XML文件中的方向?

Android layout 如何在onConfigurationChanged方法中以编程方式更新XML文件中的方向?,android-layout,Android Layout,我有一个名为activity_main的xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vert

我有一个名为activity_main的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"
 >
<fragment android:name="com.example.shko_00.myapp.MapsActivity"
    android:id="@+id/lm_fragment"
    android:layout_weight="0.5"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<fragment android:name="com.example.shko_00.myapp.GPSActivity"
 android:id="@+id/pm_fragment"
android:layout_weight="0.5"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>
</LinearLayout>


我想问一下,当方向更改为横向时,是否有任何方法可以更新android:在OnConfiguration changed方法中以编程方式从垂直方向更新为水平方向?

在这里更改方向不是正确的方法。您应该有另一个文件名为\u layout\u name-land。在该文件中使用水平方向。

在询问之前,您是否做了一些研究?这听起来很普通,所以在互联网上可能已经有这样的例子了。检查如何提问。当然,我添加了另一个文件activity_main_land。当屏幕更改为land scape时,它会工作,但当它返回到应用程序不工作时,它会工作。以下是我的代码:@Override public void onConfigurationChanged(Configuration newConfig){super.onConfigurationChanged(newConfig);如果(newConfig.orientation==Configuration.orientation_横向){setContentView(R.layout.activity_main_land)}其他{setContentView(R.layout.activity_main);}您不需要onConfigChange()中的任何代码如果您有不同的xml用于横向模式。当设备处于纵向模式时,Android将选择具有垂直方向的默认xml。我有两个片段。xml文件中的方向是垂直的,因此,片段从上到下显示。当方向更改为横向时,片段保留在相同的顺序。但是我需要从左到右改变它们的顺序(为此,需要将方向改为水平而不是垂直)。因此,我想在设备处于横向模式时将方向更改为水平方向。是否需要另一个用于横向模式的xml文件?如果需要,如何调用此文件?如果不需要,如何将垂直方向从xml文件更改为水平方向?是!使用不同的xml是实现目标的正确方法。如果您当前的xml文件名为abc.xml方向设置为水平的xml文件应命名为abc-land.xml。请阅读以供参考