6

画面のコンテンツを左に移動する必要があるため、右側にスライド メニュー用のスペースを空けることができます。 ここに画像の説明を入力

コードは次のとおりです。

// modify content layout params
    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);
    }
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
                    .getLayoutParams();
            pr.rightMargin = menuSize;
            content.setLayoutParams(pr);
// add the slide menu to parent
    parent = (FrameLayout) content.getParent();

    try {
        parent = (FrameLayout) content.getParent();
    } catch (ClassCastException e) {
        /*
         * Most probably a LinearLayout, at least on Galaxy S3.
         */
        LinearLayout realParent = (LinearLayout) content.getParent();
        parent = new FrameLayout(act);
        realParent.addView(parent, 0); // add FrameLayout to real parent of
                                        // content
        realParent.removeView(content); // remove content from real parent
        parent.addView(content); // add content to FrameLayout
    }

    LayoutInflater inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    menu = inflater.inflate(R.layout.slidemenu, null);

    FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(menuSize,
            FrameLayout.LayoutParams.MATCH_PARENT, Gravity.RIGHT);
    lays.setMargins(0, statusHeight, 0, 0);
    menu.setLayoutParams(lays);
    parent.addView(menu);

しかし、私が得るものは:
ここに画像の説明を入力

画面に合わせてコンテンツのサイズが変更されるだけですが、これを機能させるにはどうすればよいですか? ところで、サーフェスビューを使用した相対的なレイアウトであるコンテンツ レイアウトを変更することはできませんが、トップ ビュー コンテナーである DecorView を変更するため、どのレイアウトでも機能するはずです。

4

2 に答える 2

5

「画面」を移動するには、ビューで getLayoutParams() を呼び出し、必要に応じて変更してから、ビューで setLayoutParams() を呼び出す必要があります。

ただし、メニューにスライドを実装する方法の優れたチュートリアルについては、こちらを参照してください:-

http://android.cyrilmottier.com/?p=658

さらにヘルプを追加するために、ここに私があなたが求めていると思うものを達成するレイアウトがあります:-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >


</LinearLayout>

<LinearLayout
    android:layout_width="200dip"
    android:layout_height="match_parent"
    android:layout_marginLeft="-100dip"
    android:background="@color/grey_divider"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

これにより、2 番目の LinearLayout が画面の左側から 50% ずれて表示されます。参考までに、移動している LinearLayout には絶対幅が必要です。ただし、xml で FILL_PARENT に定義してから、幅を取得し、最初にマージンを負の値に設定するときにコードで絶対値として設定することができます。

お役に立てれば。

于 2013-01-22T13:47:10.890 に答える
1

親をライナーレイアウトとして使用し、左右両方のレイアウトビューのウェイトを設定します。画面からスクロールアウトする必要があるhorizo​​ntalScrollviewに左側のビューを配置します。

于 2013-01-22T13:43:04.197 に答える