【问题标题】:Customize Submenus to change background color and don't close [SherlockActionBar]自定义子菜单以更改背景颜色且不关闭 [SherlockActionBar]
【发布时间】:2013-05-22 14:41:18
【问题描述】:

我是使用 ActionBar 和 SherlockActionBar 的新手。

我要做的是向 ActionBar(AB) 添加一个“菜单”,当单击子菜单项时更改其背景颜色,并且不要关闭菜单。

原因是因为我会尝试做某种类型的“过滤器”

我加了代码,这里应该是改变背景颜色,但是不知道怎么做。

单击项目时不关闭菜单的选项我不知道是否可能,因为我从未见过。

public class MainActivity extends SherlockActivity{
    ActionBar actionBar;
    boolean boolSubitem1;
    boolean boolSubitem2;
    boolean boolSubitem3;
    boolean boolSubitem4;

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

        boolSubitem1 = false;
        boolSubitem2 = false;
        boolSubitem3 = false;
        boolSubitem4 = false;

        actionBar = getSupportActionBar();
        actionBar.setTitle("Testing");
        actionBar.setSubtitle("Miau");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId()) {

        case R.id.item1:
            return true;
        case R.id.subitem1:
            if(boolSubitem1) {
                //CHANGE BACKGROUNDCOLOR
            }
            return true;
        case R.id.subitem2:
            if(boolSubitem2) {
                //CHANGE BACKGROUNDCOLOR
            }
            return true;
        case R.id.subitem3:
            if(boolSubitem3) {
                //CHANGE BACKGROUNDCOLOR
            }
            return true;
        case R.id.subitem4:
            if(boolSubitem4) {
                //CHANGE BACKGROUNDCOLOR
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }

以及菜单的XML文件:

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

    <item
        android:id="@+id/item1"
        android:orderInCategory="0"
        android:title="Test1"
        android:showAsAction="ifRoom">
        <menu>
            <item
                android:id="@+id/subitem1"
                android:title="SubMenu1"/>
            <item
                android:id="@+id/subitem2"
                android:title="SubMenu2"/>
            <item
                android:id="@+id/subitem3"
                android:title="SubMenu3"/>
            <item
                android:id="@+id/subitem4"
                android:title="SubMenu4"/>
        </menu>
    </item>

</menu>

谢谢大家!

【问题讨论】:

    标签: android actionbarsherlock customization background-color


    【解决方案1】:

    我找到了一个有点像你的问题的解决方案,因为我想更改溢出菜单文本颜色和背景颜色。

    我只是在我的MainActivity.java 中覆盖onCreateView(String name, Context context, AttributeSet args) 并验证创建的View 是否来自android.support.v7.view.menu.ListMenuItemView 类。

    然后,我只是在里面找到TextView并更改其文本颜色(代码见下文)

    代码:

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs)  {
        // Do you own inflater stuff here 
        // Check for menu items (Options menu AKA menu key) 
        if (name.equalsIgnoreCase("android.support.v7.view.menu.ListMenuItemView")) {
            try { 
                // Ask our inflater to create the view 
                final View view = LayoutInflater.from(context).createView(name, null, attrs); 
                // Kind of apply our own background 
                new Handler().post(new Runnable() {
                    public void run() {
                        if (!Common.compatible(Common.color, 0xFF000000)) {
                            try {
                                ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFFFFFFFF);
                            } catch (ClassCastException e) {
    
                            }
                        } else {
                            try {
                                ((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFF000000);
                            } catch (ClassCastException e) {
    
                            }
                       }
                        view.setBackgroundColor(Common.color);
                    }
                });
                return view;
            } catch (InflateException e) {
    
            } catch (ClassNotFoundException e) {
    
            }
        }
        return null; 
    }
    

    来源:Change custom SubMenu background color with Java code

    【讨论】:

    • 好的,下次我会记住的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2016-05-06
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多