GVKun编程网logo

android – 动态添加/删除项菜单menuItem(android删除项目)

6

对于android–动态添加/删除项菜单menuItem感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解android删除项目,并且为您提供关于androidMenu选项菜单示例、Andro

对于android – 动态添加/删除项菜单menuItem感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解android删除项目,并且为您提供关于android Menu 选项菜单示例、Android MenuItem 分享、Android MenuItem分享、Android MenuItem自定义布局的宝贵知识。

本文目录一览:

android – 动态添加/删除项菜单menuItem(android删除项目)

android – 动态添加/删除项菜单menuItem(android删除项目)

我有一个问题,我想以编程方式在工具栏(材料设计)中添加项目.

这是我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" android:showAsAction="never" />
</menu>

我知道更改文本,颜色,背景和@override监听器

toolbar.setBackgroundColor
toolbar.setTextColor
toolbar.setNavigationIcon
toolbar.setText

我不知道如何添加菜单ítem类别“android:orderInCategory =”300“

谢谢.

注意:我有所有片段,没有1个活动

Tree - > Activity - > Fragment1(here add menu item) - > Fragment2(add/remove menu item) - > Fragmentx ..

解决方法

试试这个工具栏有选项getMenu()返回菜单
private static final String placeholder1="Something";
private static final int FirsT_ID=Menu.FirsT;
private static final int SECOND_ID=Menu.FirsT+1;


//To add an item
toolbar.getMenu().add(Menu.NONE,FirsT_ID,Menu.NONE,R.string.placeholder1);
toolbar.getMenu().add(Menu.NONE,SECOND_ID,R.string.placeholder2);

//and to remove a element just remove it with id
toolbar.getMenu().removeItem(FirsT_ID);

android Menu 选项菜单示例

ch7_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="这是一个选项菜单和子菜单的示例"/>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textSize="20px"/>

</LinearLayout>

MenuActivity.java :

package com.example.ch7;

import com.example.baseexample.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;

public class MenuActivity extends Activity {
	private TextView tv;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);;
		setContentView(R.layout.ch7_menu);
		
	}
	
	public boolean onCreateOptionsMenu(Menu menu){
		SubMenu sub = menu.addSubMenu(Menu.NONE,Menu.FIRST,0,"发送").setIcon(android.R.drawable.ic_menu_send);
		sub.add(Menu.NONE,Menu.FIRST+6,6,"发送到蓝牙");
		sub.add(Menu.NONE,Menu.FIRST+7,7,"发送到微博");
		sub.add(Menu.NONE,Menu.FIRST+8,8,"发送到E-mail");
		menu.add(Menu.NONE,Menu.FIRST+1,1,"保存").setIcon(android.R.drawable.ic_menu_edit);
		menu.add(Menu.NONE,Menu.FIRST+2,2,"帮助").setIcon(android.R.drawable.ic_menu_help);
		menu.add(Menu.NONE,Menu.FIRST+3,3,"添加").setIcon(android.R.drawable.ic_menu_add);
		menu.add(Menu.NONE,Menu.FIRST+4,4,"详细").setIcon(android.R.drawable.ic_menu_info_details);
		menu.add(Menu.NONE,Menu.FIRST+5,5,"退出").setIcon(android.R.drawable.ic_menu_delete);
		
		return true;
		
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		tv=(TextView)findViewById(R.id.tv);
		switch(item.getItemId()){
		case Menu.FIRST:
			tv.setText("你点击了发送菜单");
			break;
		case Menu.FIRST+1:
			tv.setText("你点击了保存菜单");
			break;
		case Menu.FIRST+2:
			tv.setText("你点击了帮助菜单");
			break;
		case Menu.FIRST+3:
			tv.setText("你点击了添加菜单");
			break;
		case Menu.FIRST+4:
			tv.setText("你点击了详细菜单");
			break;
		case Menu.FIRST+5:
			tv.setText("你点击了退出菜单");
			break;
		case Menu.FIRST+6:
			tv.setText("你点击了发送到蓝牙");
			break;
		case Menu.FIRST+7:
			tv.setText("你点击了发送到微博");
			break;
		case Menu.FIRST+8:
			tv.setText("你点击了发送到E-mail");
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}


Android MenuItem 分享

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.ActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        // 获取菜单项
        MenuItem item = menu.findItem(R.id.action_fuck_share);

        // 获取provider 的对象
        /**
         * ShareActionProvider 分享动作的提供者
         */
        ShareActionProvider provider = (ShareActionProvider) item
                .getActionProvider();

        // 设置分享的意图
        Intent shareIntent = new Intent();
        shareText(provider, shareIntent);

        return true;
    }

    private void shareText(ShareActionProvider provider, Intent shareIntent) {
        // 隐式跳转
        shareIntent.setAction(Intent.ACTION_SEND);
        /**
         * 设置Intent的
         */
        shareIntent.putExtra(Intent.EXTRA_TEXT, "分享的内容");

        shareIntent.setType("text/*");

        provider.setShareIntent(shareIntent);
    }

    private void shareImage(ShareActionProvider provider, Intent shareIntent) {
        // 隐式跳转
        shareIntent.setAction(Intent.ACTION_SEND);
        // 目的: 将assets 的文件,放在sd上面,再分析sd卡的图片,注意:必须要写到sd才能共享出去
        try {
            // 获取Assets下面的img文件,得到一个输入流
            InputStream is = getAssets().open("beijin.jpeg");
            // 判断挂载,和权限
            if (!Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                return;
            }
            //放在外部存储的根路径下
            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath()+File.separator+"img.jpg");
            //不存在就创建一个文件    
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
            
            int len = 0;
            byte[] buff = new byte[1024];
            while((len=is.read(buff))!=-1){
                fos.write(buff, 0, len);
            }
            fos.close();
            is.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //标识符,文件对应着的Uri
        Uri uri = Uri.fromFile(new File(Environment
                .getExternalStorageDirectory()
                .getAbsolutePath()+File.separator+"img.jpg"));
        
        /**
         * 设置Intent的
         */
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());

        shareIntent.setType("image/*");

        provider.setShareIntent(shareIntent);
    }

}


Android MenuItem分享

下面是小编 jb51.cc 通过网络收集整理的代码片段。

小编小编现在分享给大家,也给大家做个参考。

用隐式跳转手机所有有分享功能的应用。这里是简单的分享图片和文字。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.ActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main,menu);
        // 获取菜单项
        MenuItem item = menu.findItem(R.id.action_fuck_share);
 
        // 获取provider 的对象
        /**
         * ShareActionProvider 分享动作的提供者
         */
        ShareActionProvider provider = (ShareActionProvider) item
                .getActionProvider();
 
        // 设置分享的意图
        Intent shareIntent = new Intent();
        shareText(provider,shareIntent);
 
        return true;
    }
 
    private void shareText(ShareActionProvider provider,Intent shareIntent) {
        // 隐式跳转
        shareIntent.setAction(Intent.ACTION_SEND);
        /**
         * 设置Intent的
         */
        shareIntent.putExtra(Intent.EXTRA_TEXT,"分享的内容");
 
        shareIntent.setType("text/*");
 
        provider.setShareIntent(shareIntent);
    }
 
    private void shareImage(ShareActionProvider provider,Intent shareIntent) {
        // 隐式跳转
        shareIntent.setAction(Intent.ACTION_SEND);
        // 目的: 将assets 的文件,放在sd上面,再分析sd卡的图片,注意:必须要写到sd才能共享出去
        try {
            // 获取Assets下面的img文件,得到一个输入流
            InputStream is = getAssets().open("beijin.jpeg");
            // 判断挂载,和权限
            if (!Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                return;
            }
            //放在外部存储的根路径下
            File file = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath()+File.separator+"img.jpg");
            //不存在就创建一个文件    
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(file);
             
            int len = 0;
            byte[] buff = new byte[1024];
            while((len=is.read(buff))!=-1){
                fos.write(buff,len);
            }
            fos.close();
            is.close();
 
        } catch (IOException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
        //标识符,文件对应着的Uri
        Uri uri = Uri.fromFile(new File(Environment
                .getExternalStorageDirectory()
                .getAbsolutePath()+File.separator+"img.jpg"));
         
        /**
         * 设置Intent的
         */
        shareIntent.putExtra(Intent.EXTRA_STREAM,uri.toString());
 
        shareIntent.setType("image/*");
 
        provider.setShareIntent(shareIntent);
    }
 
}

以上是小编(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给程序员好友。

Android MenuItem自定义布局

当我点击操作栏中的操作按钮时,我会显示一个 PopupMenu.
我想在我的PopupMenu中的MenuItem,使用这样的自定义布局:

布局/ menu_item_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/menuItemLayout"

    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageViewMenuItem"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:src="@drawable/abc_list_focused_holo" />

    <TextView
        android:id="@+id/textViewMenuItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextViewMenuItem" />

</LinearLayout>

这是PopUpMenu的xml:

菜单/ pop_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       tools:context="apparound.actiobarpopupstylefacebook.Main" >

    <item
        android:id="@+id/popupItem"
        android:showAsAction="ifRoom"/>
</menu>

在我的活动代码如下:

public void showPopup(int idR){
View menuItemView = findViewById(idR);
PopupMenu popup = new PopupMenu(this,menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.pop_menu,popup.getMenu());
MenuItem menuItem= popup.getMenu().findItem(R.id.popupItem);
menuItem.setActionView(R.layout.menu_item_layout);
popup.show();
}

但是当弹出菜单出现时,项目为空.
我使用setActionview()方法是错误的?
谢谢.

解决方法

对于自定义布局,您不能使用菜单,一个备用选项是 PopupWindow
PopupWindow popupwindow_obj = popupdisplay();
popupwindow_obj.showAsDropDown(clickbtn,-40,18); // where u want show on view click event popupwindow.showAsDropDown(view,x,y);

public PopupWindow popupdisplay() 
{ 

    final PopupWindow popupWindow = new PopupWindow(this);

    // inflate your layout or dynamically add view
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View view = inflater.inflate(R.layout.mylayout,null);

    Button item = (Button) view.findViewById(R.id.button1);

    popupWindow.setFocusable(true);
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);

    return popupWindow;
}

在名为my layout.xml的res / layout文件夹中创建此XML文件

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Window test" />
</LinearLayout>

关于android – 动态添加/删除项菜单menuItemandroid删除项目的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android Menu 选项菜单示例、Android MenuItem 分享、Android MenuItem分享、Android MenuItem自定义布局等相关知识的信息别忘了在本站进行查找喔。

本文标签: