當前位置:首頁 » 安卓系統 » androidmenu自定義

androidmenu自定義

發布時間: 2022-05-23 09:31:16

Ⅰ 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的文字有兩種設置方式:

  1. 在 menu.xml中設置。

  2. 在java代碼onCreateOptionsMenu中設置。

以下為示例代碼:

  1. 在menu.xml中指定title即可

    <item android:id="@+id/menu"
    android:title="文字"/>

  2. 重載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);
}
}

熱點內容
虛擬存儲器尋找輔存 發布:2025-08-22 09:10:40 瀏覽:879
農村醫保金融卡密碼是多少 發布:2025-08-22 08:33:59 瀏覽:662
iphone8手機如何快捷鍵清除緩存 發布:2025-08-22 08:21:57 瀏覽:425
linux編程java 發布:2025-08-22 07:57:40 瀏覽:310
steam刪文件夾 發布:2025-08-22 07:57:38 瀏覽:52
bytec語言 發布:2025-08-22 07:37:34 瀏覽:387
蘋果手機怎麼上傳視頻到qq空間 發布:2025-08-22 07:10:03 瀏覽:638
淘寶androidsdk 發布:2025-08-22 06:52:04 瀏覽:940
編程掙錢嗎 發布:2025-08-22 06:31:21 瀏覽:1003
敬請存儲 發布:2025-08-22 06:25:42 瀏覽:612