【问题标题】:change background color single item menu更改背景颜色单项菜单
【发布时间】:2016-12-30 12:18:16
【问题描述】:

我想要这样的弹出菜单

点击按钮后显示

XML

    <item

        android:state_pressed="true"
        android:id="@+id/fromFirstMonth"
        android:title="از ابتدای سال"
        android:drawable="@drawable/nav_item_background"/>
    <item
        android:state_pressed="true"
        android:id="@+id/currentMonth"
        android:title="این ماه"
        android:drawable="@color/blueMenu"/>
    <item
        xmlns:showAsAction="always"
        android:id="@+id/currentSession"
        android:title="این فصل"

        android:drawable="@color/white"/>
    <item
        xmlns:showAsAction="always"
        android:id="@+id/selection"
        android:title="تانتخابی"
        android:drawable="@color/blueMenu"/>

</menu>

Java

   hourglass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(Products.this, hourglass);
                //Inflating the Popup using xml file
                popup.getMenuInflater().inflate(R.menu.hourglass_item, popup.getMenu());

                //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(Products.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });

                popup.show();//showing popup menu


            }
        });

我想为每个项目设置不同的背景颜色。我设置了android:drawable,但这不起作用。

我可以使用此代码更改文本颜色,但我不知道如何更改背景颜色项目

 Menu menu=popup.getMenu();
MenuItem item = menu.getItem(0);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
        item.setTitle(s);

【问题讨论】:

    标签: android menu


    【解决方案1】:

    如果您想使用item 标记并执行此操作,作为一种替代 方式,您可以尝试BackgroundColorSpan 并编写逻辑来重新格式化你的字符串。

    s.setSpan(new BackgroundColorSpan(Color.RED), 0, s.length(), 0);
    

    输出


    否则

    如果我这样做,我会创建一个没有标题的 Dialog

    private Dialog fakeDialogUseInstedOfMenuItem;
    
    
    fakeDialogUseInstedOfMenuItem = new Dialog(MainActivity.this);
    fakeDialogUseInstedOfMenuItem.requestWindowFeature(Window.FEATURE_NO_TITLE); // no Title
    fakeDialogUseInstedOfMenuItem.setContentView(R.layout.my_custom_view);
    

    并根据需要为其设置固定或滚动视图

    my_custom_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:orientation="vertical">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#789"
                android:orientation="vertical">
    
                <LinearLayout
                    android:id="@+id/first_lin"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center">
    
                    <TextView
    
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Option One"
                        android:textColor="#000"
                        android:textStyle="bold" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="#FFF"
                    android:gravity="center">
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Option Two"
                        android:textColor="#000"
                        android:textStyle="bold" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"></LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="#FFF"></LinearLayout>
            </LinearLayout>
        </ScrollView>
    
    
    </LinearLayout>
    

    并访问这样的项目,

    LinearLayout linearLayoutOneInDialog = (LinearLayout) fakeDialogUseInstedOfMenuItem.findViewById(R.id.first_lin);
    

    输出:

    但这些都是可选的方式

    【讨论】:

    • 这段代码只改变文本的背景颜色,不改变项目的整个宽度。
    • @chat kai 是的!这就是为什么我说它保留它作为替代并重新格式化字符串的大小以保持所有项目的大小相同
    • 谢谢,现在如何重新格式化这个尺寸的字符串来做同样尺寸的项目?
    • 使用代码创建此项目,获取最大大小字符串,决定要分配多少空间,根据大小获取其他字符串大小,与max one 创建新字符串并加载它们
    • 今天我学到了一个教训,试图在这里提供帮助>stackoverflow.com/a/41376728/5188159 算法绰绰有余,我告诉你逻辑思考和实现它。如果你不喜欢它找别的东西:)跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多