0

有这样的设计:

在此处输入图像描述

如何将滑动菜单图标放在左上角?另外,我确实想在屏幕上添加滑动功能:

在此处输入图像描述

到目前为止,我的代码被分解为activity_main.xml 布局、FlyOutContainer实现飞越/滑动菜单的MainActivity.java 文件,最后是作为应用程序本身主干的 .java 文件。

活动主

<com.example.btshome.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#444488"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="Button 1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="Button 2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="Button 3" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#888888"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="this is some text that you will see" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="toggleMenu"
            android:text="Toggle Menu" />
    </LinearLayout>

</com.example.btshome.FlyOutContainer>

FlyOutContainer

public class FlyOutContainer extends LinearLayout{

    private View menu;
    private View content;

    protected static final int menuMargin =100;
    public enum MenuState{
        CLOSED,OPEN
    };

    protected int currentContentOffset = 0;
    protected MenuState menuCurrentState = MenuState.CLOSED;

    public FlyOutContainer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public FlyOutContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public FlyOutContainer(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    protected void onAttachedToWindow(){
        super.onAttachedToWindow();
        this.menu = this.getChildAt(0);
        this.content = this.getChildAt(1);
        this.menu.setVisibility(View.GONE);
    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom){
        if(changed)
            this.calculateChildDimensions();
        this.menu.layout(left, top, right - menuMargin, bottom);

        this.content.layout(left + this.currentContentOffset, top, right + this.currentContentOffset, bottom);

    }

    public void toggleMenu(){
        switch(this.menuCurrentState){
        case CLOSED:
            this.menu.setVisibility(View.VISIBLE);
            this.currentContentOffset = this.getMenuWidth();
            this.content.offsetLeftAndRight(currentContentOffset);
            this.menuCurrentState = MenuState.OPEN;
            break;
        case OPEN:
            this.content.offsetLeftAndRight(-currentContentOffset);
            this.currentContentOffset = 0;
            this.menuCurrentState = MenuState.CLOSED;
            this.menu.setVisibility(View.GONE);
            break;
        }
        this.invalidate();
    }

    private int getMenuWidth() {
        // TODO Auto-generated method stub
        return this.menu.getLayoutParams().width;
    }

    private void calculateChildDimensions() {
        // TODO Auto-generated method stub
        this.content.getLayoutParams().height = this.getHeight();
        this.content.getLayoutParams().width = this.getWidth();

        this.menu.getLayoutParams().width = this.getWidth()-menuMargin;
        this.menu.getLayoutParams().height = this.getHeight();
    }
}

主要活动

public class MainActivity extends Activity {
FlyOutContainer root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_main, null);

        this.setContentView(root);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void toggleMenu(View v){
        this.root.toggleMenu();
    }

}

基本上,FlyOutContainer扩展LinearLayout并利用了现有的LinearLayout放置功能。我确实尝试让内容屏幕对onClick事件做出反应,但这显然不起作用。谁能帮我得到我想要的设计?和滑动屏幕功能。

4

3 回答 3

1

这些链接将对其他会来这里的人有所帮助。

http://www.youtube.com/watch?v=N0HNMK8xewE

http://www.dreamincode.net/forums/topic/270319-how-to-create-a-slide-in-menulist-like-in-facebook-app/

于 2013-09-17T05:30:19.173 回答
0

您可以使用带有自定义图像的搜索栏来看起来像您想要的那样

将您的布​​局包装在垂直线性布局中,具有滑块和当前布局

于 2013-04-17T02:50:33.947 回答
0

也许您需要将以下代码添加到您的 onCreat() 方法中?

setSlidingActionBarEnabled(false); 
于 2013-04-26T01:34:03.903 回答