如何在点击时在Android应用程序中显示菜单项

时间:2014-04-21 13:49:10

标签: android

我必须在android中创建一个列表菜单项,其中包含列表视图中的三个菜单项我想在每个菜单上单击显示一些内容。这是我到目前为止所做的屏幕.. enter image description here

这是我创建的菜单。我希望我会点击Menu1,而Menu1将显示在页面中..我正在发布我的代码..

 public class MainActivity extends ListActivity {

String[] menus={"Menu1","Menu2","Menu3"};

@Override
protected void onListItemClick(ListView l,View v,int position,long id){
    super.onListItemClick(l,v,position,id);

    String name=menus[position];

    try{
        Class main=Class.forName("com.betacoading.createlistmenu.app"+name);
        Intent intent=new Intent(MainActivity.this,main);
        startActivity(intent);

    }catch(ClassNotFoundException e){
       e.printStackTrace();
    }

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 //   setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,menus));
}


@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;
  }






@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
   }

   } 




public class Menu_1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_1);

    Button back=(Button)findViewById(R.id.button);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(in);
        }
    });
}

}


public class Menu_2 extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_2);

    Button back=(Button)findViewById(R.id.button);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(in);
        }
    });
    }

   }




public class Menu_3 extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_3);

    Button back=(Button)findViewById(R.id.button);

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(in);
        }
    });
}

}


AndroidManifest.xml

   <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.betacoading.createlistmenu.app" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.betacoading.createlistmenu.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu_1"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>
    <activity
        android:name=".Menu_2"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>
    <activity
        android:name=".Menu_3"
        android:label="@string/app_name" >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>
   </application>

   </manifest>



Menu_1.xml


<?xml version="1.0" encoding="utf-8"?>

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


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Menu 1"
    android:textSize="24dp"
    android:layout_gravity="center_horizontal"

    />

<Button
    android:id="@+id/button"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:text="Back"
    android:textSize="16dp"
    />




</LinearLayout>


Menu2.xml



    <?xml version="1.0" encoding="utf-8"?>

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


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Menu 2"
    android:textSize="24dp"
    android:layout_gravity="center_horizontal"

    />

<Button
    android:id="@+id/button"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:text="Back"
    android:textSize="16dp"
    />




 </LinearLayout>


menu3.xml


  <?xml version="1.0" encoding="utf-8"?>

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


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Menu 3"
    android:textSize="24dp"
    android:layout_gravity="center_horizontal"

    />

<Button
    android:id="@+id/button"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:text="Back"
    android:textSize="16dp"
    />

这是我迄今为止尝试过的代码..请帮助我让它变得动态

1 个答案:

答案 0 :(得分:1)

试试此链接

http://www.androidviews.net/2013/04/sliding-layer/

这将帮助您通过大量动画从底部,左侧,右侧和顶部制作滑动菜单。

您可以在其中放置任何内容,并在您的java类中轻松处理它。

希望它会有所帮助。