androidmenu自定义
Ⅰ Android 怎么实现自定义menuItem
可以在xml中自定义item项目为menu
Ⅱ 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(R.layout.main); 08. } 09. @Override 10. public boolean onCreateOptionsMenu(Menu menu) { 11. super.onCreateOptionsMenu(menu); 12. MenuInflater inflater = new MenuInflater(getApplicationContext()); 13. inflater.inflate(R.menu.options_menu, menu); 14. setMenuBackground(); 15. return true; 16. } 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(R.layout.main); 08. } 09. @Override 10. public boolean onCreateOptionsMenu(Menu menu) { 11. super.onCreateOptionsMenu(menu); 12. MenuInflater inflater = new MenuInflater(getApplicationContext()); 13. inflater.inflate(R.menu.options_menu, menu); 14. setMenuBackground(); 15. return true; 16. } 上面的例子可以轻松的替换当前Activity的Menu背景颜色,这里Android开发网再次提醒大家上面加粗的包名不能随意改动,如果非原生的Android系统,这句可能根据各个厂商编译的固件来灵活处理。
Ⅲ 安卓编程怎样自定义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>
Ⅳ 请教一个Android方面在Menu菜单里定义的RadioGroup中返回某个RadioButton的选中状态的问题
RadioButton在做表单的时候经常用到,在安卓开发中,RadioButton需要和RadioGroup一起使用,表示在一组可选项中,只有一个可以被选中,RadioGroup状态改变的一个监视器OnCheckedChangeListener,RadioGroup使用的时候调用setOnCheckedChangeListener(),然后重写OnCheckedChangeListener中的onCheckedChanged()方法,比如:
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 获取变更后的选项的ID
int radioButtonId = group.getCheckedRadioButtonId();
switch (radioButtonId) {
case R.id.message_radiobtn:
mFragment = new MessageFragment();
break;
case R.id.contact_radiobtn:
mFragment = new ContactFragment();
break;
case R.id.dynamic_radiobtn:
mFragment = new DynamicFragment();
break;
default:
break;
}
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.realtabcontent, mFragment).commit();
}
});
Ⅳ android menu item的字体样式和背景颜色怎么自定义
android旋钮背景颜色 android按钮背景颜色 默认情况下,Button使用android系统提供的默认背景。因此在不同平台上或者设备上,button显示的风格也不相同。android支持修改button默认的显示风格,可通过Drawable状态列表替换默认的背景
Ⅵ 怎样在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
新建自定义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>
运行效果图:
Ⅷ android menu怎样设置item的文字
Android Menu的文字有两种设置方式:
在 menu.xml中设置。
在java代码onCreateOptionsMenu中设置。
以下为示例代码:
在menu.xml中指定title即可
<item android:id="@+id/menu"
android:title="文字"/>重载onCreateOptionsMenu(Menu menu)方法,并调用API。
menu.add((int groupId, int itemId, int order, charsequence title) .setIcon(drawable ID)
add()方法的四个参数,依次是:
1、组别,如果不分组的话就写Menu.NONE。
2、Id,这个很重要,Android根据这个Id来确定不同的菜单 。
3、顺序,哪个菜单项在前面由这个参数的大小决定 。
4、文本,菜单项的显示文本。
Ⅸ 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菜单,能否给一个示例最好
public class ActivityMenu extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
...
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
....
return super.onOptionsItemSelected(item);
}
}