3

我有一个 LinearLayout,其中包括一个 Fragment 和一个 FrameLayout。我想为它们使用 layout_weight,因为我希望 Fragment 只占据屏幕的一小部分,而 FrameLayout 应该占据屏幕的大部分。我希望 layout_weight 仅应用于宽度部分而不是高度。当平板电脑的方向发生变化时,我还希望 layout_weight 发生变化。例如。当它处于纵向模式时,我希望 Fragment 占据屏幕空间的 3/10,FrameLayout 占据 7/10,而在横向模式下,我希望 Fragment 占据屏幕空间的 2/10,FrameLayout 占据 8 /10th 屏幕空间。

我有两个单独的横向和纵向模式布局,但它不起作用。我不确定这里有什么错误:-(

布局大地\frame_main_activity.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="10"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:paddingRight="10dip" >

    <!-- This fragment takes the left area fixed for menu -->

    <fragment
        android:id="@+id/left_pane_fragment"
        android:name="MenuFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <!-- A place holder which is filled by relevant content later -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:background="#FFFFFF" />
</LinearLayout>

布局大端口frame_main_activity.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="10"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:paddingRight="10dip" >

    <!-- This fragment takes the left area fixed for menu -->

    <fragment
        android:id="@+id/left_pane_fragment"
        android:name="MenuFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

    <!-- A place holder which is filled by relevant content later -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7"
        android:background="#FFFFFF" />
</LinearLayout>

问题是,它为两个方向选择任何一个 layout_weight 并且不会在方向更改时更新 layout_weight 。例如,在上述情况下,它只会为横向和纵向模式选择 2,8 的 layout_weight,并且不会根据方向变化更新它。

我不确定这里出了什么问题。我检查了各种教程,并在各种论坛上尝试了很多东西,但找不到任何可以解决问题的东西。

4

2 回答 2

3

So the correct solution which I found for the intended behavior is handling orientation changes programmatically.

In the onConfigurationChanged of my Activity, I have the following code :-

// Checks the orientation of the screen
View leftPaneFragment = findViewById(R.id.left_pane_fragment);
View contentFrame = findViewById(R.id.content_frame);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
leftPaneFragment.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 8f));
contentFrame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 2f));
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
leftPaneFragment.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 7f));
contentFrame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 3f));
}

In addition, I have also added the following in the AndroidManifest.xml:-

android:configChanges="orientation|screenLayout|screenSize"

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

References :- http://developer.android.com/guide/topics/manifest/activity-element.html http://developer.android.com/guide/topics/resources/runtime-changes.html

于 2014-05-22T17:51:12.160 回答
0

确保您没有在 AndroidManifest.xml 的调用活动中添加属性android:configChanges="orientation|screenLayout|screenSize"

于 2014-05-22T09:02:07.137 回答