修改上下文菜单样式

作者: Tom_Ji | 来源:发表于2019-05-23 16:11 被阅读6次

最近的工作中需要更改一些界面效果,遇到了上下文菜单ContextMenu,就去搜索了一下如何更改上下文菜单的效果,但是很可惜,没有找到有效的办法,不过最后还是有办法来进行修改,这里记录一下,方便自己遇到类似的问题,也可以很迅速的去处理。

有关于上下文菜单的介绍和用法,网络上很多,我这里简单的说一下,主要就是三个方法:

  1. registerForContextMenu();
  2. onCreateContextMenu();
  3. unregisterForContextMenu();

registerForContextMenu()注册需要弹出上下文菜单的 view,onCreateContextMenu()创建弹出菜单,unregisterForContextMenu()解注册。通过这三个方法,就可以很简单的实现长按弹出上下文菜单的效果了。
这里就不放图了,还是很简单的。

修改样式

未修改的样式如图所示:

修改前样式.jpg

修改后的样式:

修改后样式.jpg

修改的文件在framework中,文件的路径为framework/base/core/res/res/layout

找到名称为popup_menu_header_item_layout.xml的文件,这个为上下文菜单的head,如果没有用到,可以不用修改,对应图片中的“小米手机”

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:minWidth="196dip"
    android:paddingStart="16dip"
    android:paddingEnd="16dip">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?attr/textAppearancePopupMenuHeader"
        android:layout_gravity="center_vertical"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:textAlignment="viewStart" />

</FrameLayout>

在这个文件中可以按照要求进行修改,因为head不需要获取焦点,所以直接设置背景色和字体颜色就可以了,这个就是基本操作了,很简单,这里就不说了。

找到名称为popup_menu_item_layout.xml的文件,这个为上下文菜单的item,对应图片中的“连接到网络”。

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<com.android.internal.view.menu.ListMenuItemView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:minWidth="196dip"
    android:paddingEnd="16dip">
    
    <!-- Icon will be inserted here. -->
    
    <!-- The title and summary have some gap between them, and this 'group' should be centered vertically. -->
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="16dip"
        android:duplicateParentState="true">
        
        <TextView 
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:textAppearance="?attr/textAppearanceLargePopupMenu"
            android:singleLine="true"
            android:duplicateParentState="true"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:textAlignment="viewStart" />

        <TextView
            android:id="@+id/shortcut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:layout_alignParentStart="true"
            android:textAppearance="?attr/textAppearanceSmallPopupMenu"
            android:singleLine="true"
            android:duplicateParentState="true"
            android:textAlignment="viewStart" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/submenuarrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:scaleType="center"
        android:visibility="gone" />

    <!-- Checkbox, and/or radio button will be inserted here. -->
    
</com.android.internal.view.menu.ListMenuItemView>

这个文件的修改如果不细心,容易造成多次修改,首先是背景色,我的需求是获取焦点和没有焦点时,背景色是不一样的,这个可以定义一个selector来实现,字体颜色也是一样的,注意要把title的高度改为match_parent,并且设置为垂直居中,还有margin属性,也是需要注意的地方,修改完后就实现了效果。

验证修改结果

修改了framework的代码,要如何验证结果呢?

  1. 编译res 使用命令mm编译res
  2. 在out目录下找到framework-res.apk
  3. 将framework-res.apk push到/system/framework/
  4. reboot

完成以上步骤就可以看到效果了。

ps:我这里是源码编译的。

如果有能够在应用层就可以达到修改上下文菜单的要求,烦请评论或私信我,如果有文章,也可以推荐给我,让我学习一下

相关文章

  • 修改上下文菜单样式

    最近的工作中需要更改一些界面效果,遇到了上下文菜单ContextMenu,就去搜索了一下如何更改上下文菜单的效果,...

  • ElementUI笔记

    //【树结构下拉菜单的样式修改】 //【Tree 树形控件】 1、选中样式 //【element-ui中dialo...

  • semantic 样式修改

    需求:在原有默认样式的基础上,想要自定义网站的样式。 一、修改less文件 举个例子,如果我想修改菜单的样式,在s...

  • 使用Total Commander提高工作效率

    Everything 通过修改配置文件, 使用total Commander打开文件夹工具->选项->上下文菜单“...

  • 2019-04-23 Day8 MOS训练营

    项目13: 任务1: 开始菜单——单元格样式——我的标题——(右键点我的标题,选择修改)——样式“格式”——边框—...

  • windows 文件权限变更

    改变权限 添加上下文菜单 InstallTakeOwnership.reg 删除上下文菜单 RemoveTakeO...

  • 菜单Menu(选择,上下,侧滑)

    选择菜单 一.OptionMenu 上下文菜单 侧滑菜单

  • ContextMenu 上下文菜单(二)

    Babybus-u3d技术交流-ContextMenu 上下文菜单(二) ContextMenu 上下文菜单(二)...

  • Teamcenter菜单栏开发

    菜单分为主菜单,上下文菜单,视图菜单 主菜单扩展:

    HTML5事件

    1. contextmenu事件 显示上下文菜单的,以便开发人员取消默认的上下文菜单而提供自己的菜单。contex...

网友评论

    本文标题:修改上下文菜单样式

    本文链接:https://www.haomeiwen.com/subject/kkhbzqtx.html