当前位置:首页 » 安卓系统 » android自定义menu

android自定义menu

发布时间: 2022-08-04 03:58:46

① 如何在 Android Studio 下创建 Menu

当需要创建一个 Menu 布局文件时,如果直接在 res 文件夹右键选择 XML 文件添加

直接创建.xml文件
会发现虽然可以创建,但是新建的布局文件似乎有点奇怪,不能添加 menu 以及 item

无法正常使用
解决办法:
出现这种情况,可以尝试使用下面这种方法来进行 menu 布局文件的创建:

Android resource directory

menu
在新创建出来的 menu 文件上再次右键

Menu resource file

输入 File name
到这里就已经完成了,可以正常的使用了。

② android studio 新建menu文件夹怎么设置

一、问题:
android studio项目中没有看到menu文件夹:

在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下图:

但是会发现新建的布局文件好像很奇怪,不能添加menu以及item

③ 怎样在Android Menu item中使用自定义View

1.自定义属性:attrs.xml

2.MenuItemView.java源码

package com.dandy.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.lingyun.switchbutton.R;

/**
* 设置,菜单中的布局项
* 默认控件是纵向居中显示,所有paddingTop和paddingBottom在这没有作用
* Created by dandy on 2016/4/14.
*/
public class MenuItemView extends View{

private static final String TAG = "MenuItemView";

/**默认是按下状态的最小值**/
private static final long PRESSED_TIMEOUT = 10;

/**默认控件距离边界的大小**/
private static final int PADDING_DEFAULT = 18;

/**默认控件的高**/
private static final int HEIGHT_DEFAULT = 50;

/**文字绘制时默认大小**/
private static final int TEXTSZIE_DEFAULT = 14;

/**尾部箭头图标大小**/
private static final int ARROW_SIZE = 13;

/***SwitchButton默认宽*/
private static final int SWITCHBUTTON_WIDTH = 50;

/***SwitchButton默认高*/
private static final int SWITCHBUTTON_HEIGHT = 28;

/**头标**/
private Drawable headerDrawable;

/**头标宽**/
private int headerDrawableWidth;
/**头标高**/
private int headerDrawableHeight;

/**距离左边缘的距离**/
private int paddingLeft;

/**距离右边缘的距离**/
private int paddingRight;

/**绘制头标时,画布在Y轴的绘制偏移量**/
private float headerDrawableStartDrawY;

/**文字与图片间的距离**/
private int drawablePadding = -1;

/**头部文字提示**/
private String textHeader;

/**文字颜色**/
private int textHeaderColor = Color.parseColor("#5a5a5a");

/**文字大小**/
private int textSize = -1;

/**文字绘制时,画布在Y轴的绘制偏移量**/
private float textStartDrawY;

/**绘制文字的画笔**/
private Paint textPaint;

/** 尾部 > 图片**/
private Drawable arrowDrawable;
/**尾部 > 大小**/
private int arrowSize = -1;

/** > 绘制的X轴偏移量**/
private float arrowStartDrawX;

/** > 绘制的Y轴偏移量**/
private float arrowStartDrawY;

/**footerDrawable != null 时,绘制的时候是否按照原图片大小绘制**/
private boolean arrowWropToSelf = true;

/**尾部宽**/
private int arrowDrawableWidth;
/**尾部高**/
private int arrowDrawableHeight;

/**绘制arrow画笔**/
private Paint arrowPaint;

/**arrowPaint 颜色**/
private int arrowColor = Color.parseColor("#5a5a5a");

private DisplayMetrics dm;

/*以下是绘制SwitchButton所用到的参数*/

private Style style = Style.CUSTOM_ITEM;

/**默认宽**/
private int switchButtonWidth = -1;

/**默认高**/
private int switchButtonHeight = -1;

private static final long DELAYDURATION = 10;

/**开启颜色**/
private int onColor = Color.parseColor("#4ebb7f");
/**关闭颜色**/
private int offColor = Color.parseColor("#dadbda");
/**灰色带颜色**/
private int areaColor = Color.parseColor("#dadbda");
/**手柄颜色**/
private int handlerColor = Color.parseColor("#ffffff");
/**边框颜色**/
private int borderColor = offColor;
/**开关状态**/
private boolean toggleOn = false;
/**边框宽**/
private int borderWidth = 2;
/**纵轴中心**/
private float centerY;
/**按钮水平方向开始、结束的位置**/
private float startX,endX;
/**手柄x轴方向最小、最大值**/
private float handlerMinX,handlerMaxX;
/**手柄大小**/
private int handlerSize;
/**手柄在x轴的坐标位置**/
private float handlerX;
/**关闭时内部灰色带宽度**/
private float areaWidth;
/**是否使用动画效果**/
private boolean animate = true;
/**是否默认处于打开状态**/
private boolean defaultOn = true;
/**按钮半径**/
private float radius;

/**整个switchButton的区域**/
private RectF switchRectF = new RectF();

/**绘制switchButton的画笔**/
private Paint switchPaint;

private OnToggleChangedListener mListener;

private Handler mHandler = new Handler();

private double currentDelay;

private float downX = 0;

/**switchButton在X轴绘制的偏移量**/
private float switchButtonDrawStartX;

/**switchButton在Y轴绘制的偏移量**/
private float switchButtonDrawStartY;

/**分割线,默认在底部绘制**/
private Drawable dividerr;

/**分割线绘制的宽**/
private int dividerWidth = 2;

/**是否需要绘制分割线**/
private boolean dividerVisibilty = true;

/**触摸事件是否完成**/
private boolean touchDownEnd = false;

public MenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setup(attrs);
}
public MenuItemView(Context context, AttributeSet attrs) {
super(context, attrs);
setup(attrs);
}

/**
* 初始化控件,获取相关的控件属性
* @param attrs
*/
private void setup(AttributeSet attrs){

dm = Resources.getSystem().getDisplayMetrics();

TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MenuItemView);
if(typedArray != null){
int count = typedArray.getIndexCount();
for(int i = 0;i < count;i++){
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.MenuItemView_headerDrawable:
headerDrawable = typedArray.getDrawable(attr);
break;
case R.styleable.MenuItemView_drawPadding:
drawablePadding = typedArray.getDimensionPixelSize(attr,drawablePadding);
break;
case R.styleable.MenuItemView_textHeader:
textHeader = typedArray.getString(attr);
break;
case R.styleable.MenuItemView_textHeaderColor:
textHeaderColor = typedArray.getColor(attr, textHeaderColor);
break;
case R.styleable.MenuItemView_textSize:
textSize = typedArray.getDimensionPixelSize(attr, textSize);
break;
case R.styleable.MenuItemView_arrowDrawable:
arrowDrawable = typedArray.getDrawable(attr);
break;
case R.styleable.MenuItemView_arrowSize:
arrowSize = typedArray.getDimensionPixelSize(attr, arrowSize);
break;
case R.styleable.MenuItemView_arrowWropToSelf:
arrowWropToSelf = typedArray.getBoolean(attr, true);
break;
case R.styleable.MenuItemView_arrowColor:
arrowColor = typedArray.getColor(attr, arrowColor);
break;
case R.styleable.MenuItemView_onColor:
onColor = typedArray.getColor(attr, onColor);
break;
case R.styleable.MenuItemView_offColor:
borderColor = offColor = typedArray.getColor(attr,offColor);
break;
case R.styleable.MenuItemView_areaColor:
areaColor = typedArray.getColor(attr, areaColor);
break;
case R.styleable.MenuItemView_handlerColor:
handlerColor = typedArray.getColor(attr, handlerColor);
break;
case R.styleable.MenuItemView_bordeWidth:
borderWidth = typedArray.getColor(attr, borderWidth);
break;
case R.styleable.MenuItemView_animate:
animate = typedArray.getBoolean(attr, animate);
break;
case R.styleable.MenuItemView_defaultOn:
defaultOn = typedArray.getBoolean(attr, defaultOn);
break;
case R.styleable.MenuItemView_Style:
style = Style.getValue(typedArray.getInt(attr, Style.CUSTOM_ITEM.ordinal()));
break;
case R.styleable.MenuItemView_switchButtonWidth:
switchButtonWidth = typedArray.getDimensionPixelOffset(attr, switchButtonWidth);
break;
case R.styleable.MenuItemView_switchButtonHeight:
switchButtonHeight = typedArray.getDimensionPixelOffset(attr, switchButtonHeight);
break;
case R.styleable.MenuItemView_dividerr:
dividerr = typedArray.getDrawable(attr);
break;
case R.styleable.MenuItemView_dividerWidth:
dividerWidth = typedArray.getDimensionPixelOffset(attr,dividerWidth);
break;
case R.styleable.MenuItemView_dividerVisibilty:
dividerVisibilty = typedArray.getBoolean(attr,dividerVisibilty);
break;
}
}
typedArray.recycle();
}

④ android中menu怎么写

菜单资源文件必须放在res/menu目录中。菜单资源文件必须使用<menu>标签作为根节点。除了<menu>标签外,还有另外两个标签用于设置菜单项和分组,这两个标签是<item>和<group>。
<menu>标签没有任何属性,但可以嵌套在<item>标签中,表示子菜单的形式。不过<item>标签中不能再嵌入<item>标签。
1.<item>标签的属性含义如下:
Id:表示菜单项的资源ID
menuCategory:同种菜单项的种类。该属性可取4个值:container、system、secondary和alternative。通过menuCategroy属性可以控制菜单项的位置。例如将属性设为system,表示该菜单项是系统菜单,应放在其他种类菜单项的后面。
orderInCategor:同种类菜单的排列顺序。该属性需要设置一个整数值。例如menuCategory属性值都为system的3个菜单项(item1、item2和item3)。将这3个菜单项的orderInCategory属性值设为3、2、1,那么item3会显示在最前面,而item1会显示在最后面。
title:菜单项标题(菜单项显示的文本)
titleCondensed:菜单项的短标题。当菜单项标题太长时会显示该属性值
icon:菜单项图标资源ID
alphabeticShortcut:菜单项的字母快捷键
numericShortcut:菜单项的数字快捷键
checkable:表示菜单项是否带复选框。该属性可设计为true或false
checked:如果菜单项带复选框(checkable属性为true),该属性表示复选框默认状态是否被选中。可设置的值为true或false
visible:菜单项默认状态是否可视
enable:菜单项默认状态是否被激活

⑤ android中怎么让menu菜单显示在屏幕左上角

用惯了Android的人在刚拿到iPhone的时候,总是会习惯性的用手指从状态栏往下拖一下,这都是给Notification闹的。
不过Notification也确实是1个不错的提示工具,不干扰正常的操作,事后还可以再翻看详细的内容,点击后还可以进入相关的画面查看更具体的内容。
今天我就以代码为主的形式来介绍Notification的使用,包括基本用法,自定义的View,以及更多的控制方法。
另一种Android中常用到的提示方法Toast的用法请参见《教程:在Android中使用Toast进行提示》
我们先看下Notification的几个主要组成部分:
Icon:不解释
Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动

Content Title:Notification展开后的标题
Content Text:Notification展开后的内容

Notification的一般用法
取得NotificationManager
private NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
创建Notification并且显示
//Notification的滚动提示
String tickerText = "My notification, It's a long text! Hello World desiyo?";
//Notification的图标,一般不要用彩色的
int icon = R.drawable.icon_02241_3;

//contentTitle和contentText都是标准的Notification View的内容
//Notification的内容标题,拖下来后看到的标题
String contentTitle="My notification";
//Notification的内容
String contentText="Hello World!";

//Notification的Intent,即点击后转向的Activity
Intent notificationIntent = new Intent(this, this.getClass());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

//创建Notifcation
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
//设定Notification出现时的声音,一般不建议自定义
notification.defaults |= Notification.DEFAULT_SOUND;
//设定如何振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
//这符合一般的Notification的运作规范
notification.flags|=Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
//显示这个notification
mNotificationManager.notify(HELLO_ID, notification);
这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。

使用自定义View的Notification
同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在Android Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。
首先给出View的定义文件:notification_view_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/notificationImage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@android:drawable/stat_sys_download"
/>
<TextView android:id="@+id/notificationTitle"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@id/notificationImage"
android:layout_alignParentRight="true"
android:paddingLeft="6dp"
android:textColor="#FF000000"
/>
<TextView android:id="@+id/notificationPercent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/notificationImage"
android:paddingTop="2dp"
android:textColor="#FF000000"
/>
<ProgressBar android:id="@+id/notificationProgress"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/notificationTitle"
android:layout_alignLeft="@id/notificationTitle"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/notificationPercent"
android:paddingLeft="6dp"
android:paddingRight="3dp"
android:paddingTop="2dp"
style="?android:attr/progressBarStyleHorizontal"
/>
</RelativeLayout>
RelativeLayout的使用,可以参考:《教程:Android各种Layout特性和使用汇总(一)》

⑥ android 自定义menu 怎么修改高度

01.public class MenuEx extends Activity { 02. 03. private static final String TAG = "android123"; 04. @Override 05. public void onCreate(Bundle savedInstanceState) { 06. super.onCreate(savedInstanceState); 07. setContentView(

⑦ android menu item的字体样式和背景颜色怎么自定义

android旋钮背景颜色 android按钮背景颜色 默认情况下,Button使用android系统提供的默认背景。因此在不同平台上或者设备上,button显示的风格也不相同。android支持修改button默认的显示风格,可通过Drawable状态列表替换默认的背景

⑧ android如何在系统导航栏旁显示menu按键

要想让menu按键显示在系统导航栏旁,需要改变AndroidManifest内的
内的最小
sdk版本。最高为10,不得高于10,否则无法现实在系统导航栏旁。个人想法是:Android3.0后加入了ActionBar控件和虚拟的按键,而ActionBar整合了menu功能导致android3.0以上的menu按钮只能现实在ActionBar上,无法显示在系统导航栏旁。

⑨ Android如何自定义Menu

新建自定义Menu————>TabMenu.java如下:

package com.ncw;

import android.content.Context;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.GridView;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.PopupWindow;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.LinearLayout.LayoutParams;

public class TabMenu extends PopupWindow {

private GridView gridView;

private LinearLayout mLayout;

public TabMenu(Context context, OnItemClickListener bodyClick, int colorBgTabMenu) {

super(context);

mLayout = new LinearLayout(context);

mLayout.setOrientation(LinearLayout.VERTICAL);

gridView = new GridView(context);

gridView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色

gridView.setNumColumns(4);

gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

gridView.setVerticalSpacing(0);

gridView.setHorizontalSpacing(0);

gridView.setPadding(0, 0, 0, 0);

gridView.setGravity(Gravity.CENTER);

gridView.setOnItemClickListener(bodyClick);

mLayout.addView(gridView);

// 设置默认项

this.setContentView(mLayout);

this.setWidth(LayoutParams.FILL_PARENT);

this.setHeight(LayoutParams.WRAP_CONTENT);

this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景

this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应

}

public void SetBodySelect(int index, int colorSelBody) {

int count = gridView.getChildCount();

for (int i = 0; i < count; i++) {

if (i != index)

((LinearLayout) gridView.getChildAt(i))

.setBackgroundColor(Color.TRANSPARENT);

}

((LinearLayout) gridView.getChildAt(index))

.setBackgroundColor(colorSelBody);

}

public void SetBodyAdapter(MenuBodyAdapter bodyAdapter) {

gridView.setAdapter(bodyAdapter);

}

/**

* 自定义Adapter,TabMenu的每个分页的主体

*

*/

static public class MenuBodyAdapter extends BaseAdapter {

private Context mContext;

private int[] resID;

/**

* 设置TabMenu的分页主体

*

* @param context

* 调用方的上下文

* @param resID

*/

public MenuBodyAdapter(Context context, int[] resID) {

this.mContext = context;

this.resID = resID;

}

@Override

public int getCount() {

return resID.length;

}

public Object getItem(int position) {

return makeMenyBody(position);

}

public long getItemId(int position) {

return position;

}

private LinearLayout makeMenyBody(int position) {

LinearLayout result = new LinearLayout(this.mContext);

result.setOrientation(LinearLayout.VERTICAL);

result.setGravity(Gravity.CENTER_HORIZONTAL

| Gravity.CENTER_VERTICAL);

result.setPadding(0, 0, 0, 0);

ImageView img = new ImageView(this.mContext);

img.setBackgroundResource(resID[position]);

result.addView(img, new LinearLayout.LayoutParams(new LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));

return result;

}

public View getView(int position, View convertView, ViewGroup parent) {

return makeMenyBody(position);

}

}

}

?

1

使用自定义Menu————>testTabMenu.java

package com.ncw;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.util.Log;

import android.view.Gravity;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

public class testTabMenu extends Activity {

TabMenu.MenuBodyAdapter bodyAdapter = new TabMenu.MenuBodyAdapter(this,

new int[] { R.drawable.menu_01, R.drawable.menu_02,

R.drawable.menu_03, R.drawable.menu_04 });

TabMenu tabMenu;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tabMenu = new TabMenu(this, new BodyClickEvent(), R.drawable.menu_bg);// 出现与消失的动画

tabMenu.update();

tabMenu.SetBodyAdapter(bodyAdapter);

}

class BodyClickEvent implements OnItemClickListener {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

tabMenu.SetBodySelect(arg2, Color.GRAY);

Log.i("Log", " BodyClickEvent implements OnItemClickListener "

+ arg2);

}

}

@Override

/**

* 创建MENU

*/

public boolean onCreateOptionsMenu(Menu menu) {

menu.add("menu");// 必须创建一项

return super.onCreateOptionsMenu(menu);

}

@Override

/**

* 拦截MENU

*/

public boolean onMenuOpened(int featureId, Menu menu) {

if (tabMenu != null) {

if (tabMenu.isShowing())

tabMenu.dismiss();

else {

tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),

Gravity.BOTTOM, 0, 0);

}

}

return false;// 返回为true 则显示系统menu

}

}


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/LinearLayout01"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView

android:id="@+id/TextView01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="自定义Menu————张亚龙" >

</TextView>

</LinearLayout>

运行效果图:

⑩ 安卓编程怎样自定义menu中的字体大小

一、先到AndroidManifest.xml看看当前的theme是什么:

比如我这里的是AppTheme

[html] view plain
<application
......
android:theme="@style/MyAppTheme" >

二、然后在资源文件的res/values/styles.xml中找到 你的主题:

[html] view plain
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">

三、然后在此添加上一个item,name=android:actionMenuTextAppearance,然后引用你自己定义的文字样式,不管是大小还是颜色都可以自己定义

[html] view plain
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionMenuTextAppearance">@style/MyMenuTextStyle</item>
</style>

<style name="MyMenuTextStyle">
<item name="android:textColor">@android:color/red</item>
<item name="android:textSize">16sp</item>
</style>

热点内容
sql多表连接语句 发布:2024-05-16 21:21:53 浏览:300
sqlscope 发布:2024-05-16 21:17:37 浏览:965
存储器通常有 发布:2024-05-16 21:17:35 浏览:378
云数据库概念 发布:2024-05-16 21:17:32 浏览:818
铺地板编程 发布:2024-05-16 21:07:36 浏览:458
苹果手机缓存删除 发布:2024-05-16 21:04:35 浏览:507
scratch编程电子书 发布:2024-05-16 21:02:54 浏览:227
wps加密文件忘记密码 发布:2024-05-16 21:01:37 浏览:931
不义联盟安卓版在哪里下载 发布:2024-05-16 21:00:42 浏览:726
微信存储空间不足怎么办 发布:2024-05-16 21:00:24 浏览:790