这篇文章将为大家详细讲解有关Android怎么制作一个app顶部menu菜单栏,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
菜单添加:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_menu_search"
android:title="@string/action_search"/>
<item
android:id="@+id/addFriend"
android:icon="@drawable/ic_menu_rotate"
android:title="@string/menu_addFrideds"/>
<item
android:id="@+id/teamChart"
android:icon="@drawable/ic_menu_refresh"
android:title="@string/menu_teamChart"/>
<item
android:id="@+id/monery"
android:icon="@drawable/ic_menu_preferences"
android:title="@string/menu_getMonery"/>
<item
android:id="@+id/look"
android:icon="@drawable/ic_menu_save"
android:title="@string/menu_look"/>
</menu>
写好这文件,我还需要在我们的res-->values文件夹下,打开一个文件名为strings.xml的文件,来配置我们的中文常量。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">满艺</string>
<string name="action_search">检索</string>
<string name="action_more">更多</string>
<string name="menu_addFrideds">添加朋友</string>
<string name="menu_teamChart">发起群聊</string>
<string name="menu_getMonery">收款</string>
<string name="menu_look">扫一扫</string>
<string name="welcome">您好,满艺</string>
</resources>
到这里我们现在运行我们的程序,局可以看到我们程序的顶部出现了类似微信的效果,左侧是应用图标+应用名称,右侧则是一个搜索按钮+表示更多的按钮。现在我们先来将系统默认自带的更多图标换成,我们定制的一个按钮。打开我们的AndroidManifest.xml,我们会发现系统默认我们应用了一个样式文件android:theme="@style/AppTheme",点击打开这个样式文件,将我们的自定义更多图标添加到样式上:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionOverflowButtonStyle">@style/menuOverflowButtonStyle</item>
</style>
<style name="menuOverflowButtonStyle">
<item name="android:src">@drawable/ic_menu_more</item>
</style>
</resources>
现在我们运行我们的工程,会发现和微信的效果还是有些不同,这是我们就要通过在MainActivity.java文件中,1通过反射机制来设置自定义更多图标显示,2重写onMenuOpened方法来设置每一个menu菜单像微信那样,显示为图标加标题的形式。
package com.example.androidmenuview;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setMenuOverflowAlways();
getActionBar().setDisplayShowHomeEnabled(false);//设置ActionBar应用图标不显示
}
//初始化Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE,Menu.FIRST+6,7,"新增").setIcon(android.R.drawable.ic_input_add);//手动添加menu菜单
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//添加Menu的点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
// Toast.makeText(this, "检索按钮", Toast.LENGTH_SHORT).show();
break;
case R.id.addFriend:
// Toast.makeText(this, "添加朋友", Toast.LENGTH_SHORT).show();
break;
case R.id.teamChart:
// Toast.makeText(this, "群聊", Toast.LENGTH_SHORT).show();
break;
case R.id.look:
// Toast.makeText(this, "扫一扫", Toast.LENGTH_SHORT).show();
break;
case R.id.monery:
// Toast.makeText(this, "收款", Toast.LENGTH_SHORT).show();
break;
case Menu.FIRST+6:
// Toast.makeText(this, "新增", Toast.LENGTH_SHORT).show();
break;
}
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
//设置menu菜单的第一个图标显示在标题右上角---使用反射机制来完成
public void setMenuOverflowAlways(){
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field field = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
field.setAccessible(true);
field.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
//设置每个Menu显示为左图标右标题
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onMenuOpened(featureId, menu);
}
}
这里我从写了onOptionsItemSelected()方法,从而为每一个menu菜单添加点击事件。
关于Android怎么制作一个app顶部menu菜单栏就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。