使用“幻灯片”菜单

时间:2014-02-24 13:12:13

标签: java android eclipse button menu

所以我在菜单中有一些幻灯片的代码,当我点击一个按钮时,我不会调出新的布局,但仍然有菜单中的幻灯片。这是我的主要活动代码:

public class MainActivity extends Activity implements OnClickListener {

ImageButton menu_button; 
Button p1, p2, p3, p4;
LinearLayout content, menu;
LinearLayout.LayoutParams contentParams;
TranslateAnimation slide;
int marginX, animateFromX, animateToX = 0;
boolean menuOpen = false;


@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);


    menu = (LinearLayout)findViewById(R.id.menu);

    content = (LinearLayout)findViewById(R.id.content);
    contentParams = (LinearLayout.LayoutParams)content.getLayoutParams();
    contentParams.width = getWindowManager().getDefaultDisplay().getWidth();    
    contentParams.leftMargin = -(menu.getLayoutParams().width);     
    content.setLayoutParams(contentParams);


    menu_button = (ImageButton)findViewById(R.id.menu_button);
    menu_button.setOnClickListener(this);



    p1 = (Button)findViewById(R.id.p1);
    p1.setOnClickListener(this);

    p2 = (Button)findViewById(R.id.p2);
    p2.setOnClickListener(this);

    p3 = (Button)findViewById(R.id.p3);
    p3.setOnClickListener(this);

    p4 = (Button)findViewById(R.id.p4);
    p4.setOnClickListener(this);    
}

public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        if(menuOpen) {
            slideMenuIn(0, -(menu.getLayoutParams().width), -(menu.getLayoutParams().width));   
            menuOpen = false;
            return true;
        }
    }
    return super.onKeyDown(keyCode, keyEvent);
}

public void onClick(View v) {
    switch(v.getId()) {
    case R.id.p1:
        // What do I put here to make this happen?
        break;
    case R.id.p2:

        break;
    case R.id.p3:

        break;
    case R.id.p4:

        break;
    }

    if(contentParams.leftMargin == -(menu.getLayoutParams().width)) {   
        animateFromX = 0;
        animateToX = (menu.getLayoutParams().width);
        marginX = 0;
        menuOpen = true;
    } else {    
        animateFromX = 0;
        animateToX = -(menu.getLayoutParams().width);
        marginX = -(menu.getLayoutParams().width);
        menuOpen = false;
    }
    slideMenuIn(animateFromX, animateToX, marginX);
}

public void slideMenuIn(int animateFromX, int animateToX, final int marginX){
    slide = new TranslateAnimation(animateFromX, animateToX, 0, 0);
    slide.setDuration(300);
    slide.setFillEnabled(true);
    slide.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(Animation animation) {       
            contentParams.setMargins(marginX, 0, 0, 0);         
            content.setLayoutParams(contentParams);
        }

        public void onAnimationRepeat(Animation animation) { }
        public void onAnimationStart(Animation animation) { }
    });
    content.startAnimation(slide);      
}
 }

这是我在幻灯片菜单中的主要XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
    android:id="@+id/menu"
    android:layout_height="fill_parent"
    android:layout_width="150dip"
    android:background="#f2f2f2"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000"
        android:layout_marginTop="50dip" />
    <Button
        android:id="@+id/p1"
        android:text="1"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:textSize="18sp"
        android:background="@drawable/states" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000" />
    <Button
        android:id="@+id/p2"
        android:text="2"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:textSize="18sp"
        android:background="@drawable/states" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000" />
    <Button
        android:id="@+id/p3"
        android:text="3"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:textSize="18sp"
        android:background="@drawable/states" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000" />
    <Button
        android:id="@+id/p4"
        android:text="3"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:textSize="18sp"
        android:background="@drawable/states" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000" />
</LinearLayout>
<LinearLayout
    android:id="@+id/content"
    android:layout_height="fill_parent"
    android:layout_width="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:orientation="horizontal"
        android:background="#f2f2f2" >
        <ImageButton
            android:id="@+id/menu_button"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:background="@drawable/states"
            android:src="@drawable/options"
            android:contentDescription="@string/app_name" />
        <TextView
            android:text="FirstApp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#333"
            android:textSize="24sp"
            android:paddingTop="14dip"
            android:gravity="center" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#000" />
    <ScrollView
         android:layout_width="match_parent"
         android:layout_height="fill_parent"
         android:background="#fff" >
        <TextView
            android:id="@+id/paragraph"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dip" />
    </ScrollView>
</LinearLayout>


  </LinearLayout>

最后,当点击幻灯片放入菜单中的第一个按钮时,这是我想要出现的Xml:

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

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:ems="10"
    android:hint="Box 1" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:ems="10"
    android:hint="Box 2" />

  </RelativeLayout>

请帮助。

0 个答案:

没有答案