[英]How to show menu item in android application on click
我必须在android中制作一个列表菜单项,其中要在列表视图中包含三个菜单项,我要在单击每个菜单时显示一些内容。这是到目前为止我所做的一个屏幕。
这是我创建的菜单。我希望我单击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"
/>
这是我到目前为止尝试过的代码。请帮助我使其动态化
试试这个连结
http://www.androidviews.net/2013/04/sliding-layer/
这将帮助您从底部,左侧,右侧和顶部制作带有许多动画的滑动菜单。
您可以在其中放置任何内容,并在java类中轻松对其进行操作。
希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.