0

画面のコンテンツを左側に移動する必要があるため、このコードを使用してコンテンツを取得します

try {
        content = ((LinearLayout) act.findViewById(android.R.id.content)
                .getParent());
    } catch (ClassCastException e) {
        /*
         * When there is no title bar
         * (android:theme="@android:style/Theme.NoTitleBar"), the
         * android.R.id.content FrameLayout is directly attached to the
         * DecorView, without the intermediate LinearLayout that holds the
         * titlebar plus content.
         */
        content = (FrameLayout) act.findViewById(android.R.id.content);
    }

Android > 3.0 のデバイスではコンテンツが try ブロックに設定され、デバイス < 3.0 では catch ブロックに設定されることに気付きました。ここでコンテンツを移動します。

FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
                    .getLayoutParams();
            pr.rightMargin = menuSize;
            pr.leftMargin = -menuSize;
            content.setLayoutParams(pr);

そのため、3.0 以上のデバイスでは、コンテンツが移動しても問題なく動作し、3.0 未満のデバイスでは何も起こらず、コンテンツは変更されずにそのままです。

4

1 に答える 1

0

重力が設定されていない場合、FrameLayoutはマージンを考慮に入れません。

于 2013-01-23T17:37:46.913 に答える