如何给android中的设置菜单中添加一个item

如何给android中的设置菜单中添加一个item,第1张

在res下新建menu文件夹,然后新建菜单文件

定义一个xml文件(在menu资源里面),

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

<item

android:id="@+id/menu_settings"

android:showAsAction="never"

android:title="设置" />

<item

android:id="@+id/menu_exit"

android:showAsAction="never"

android:title="退出" />

</menu>

(item有多少个,显示就多少个)

在代码里面的onCreateOptionsMenu方法里面把这个xml布局填充进去,代码如下:

MenuInflater inflater = getMenuInflater()

inflater.inflate(R.menu.menu_settings, menu)

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

    @Override

    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId())

        {

            case R.id.grade:

                Toast.makeText(getApplicationContext(),"你点击了第一项",Toast.LENGTH_SHORT).show()

                break

            case R.id.navcall:

                Toast.makeText(getApplicationContext(),"你点击了第二项",Toast.LENGTH_SHORT)

                .show()

                break

                default:

                    break

        }

        return true

    }

})

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 = 0i <counti++){

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()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7959710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存