自定义带图标文本的块状态、条状菜单

简介:

使用场景


相信很多移动端开发者都很熟悉了,通常我们首先想到的就是使用 RelativeLayout 方式处理,例如: 


<RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:onClick="onClickToMain"
        >
        <ImageView
            android:layout_width="@dimen/icon_len"
            android:layout_height="@dimen/icon_len"
            android:src="@drawable/selector_icon_main"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/index"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:layout_marginBottom="3dp"
            android:textColor="@color/selector_common_icon_text"
            android:textSize="@dimen/common_content_text_small"
            />
    </RelativeLayout>


我们写一个还好,当你重复写三个以上这样的控件时你可能一直在重复复制粘贴的动作,而且你的布局层次会变得复杂而冗长。相信有追求的猿猿都会想到自定义了,于是就出现了这个开源 demo。

简单使用
1)SingleBar Style(根据需要指定右边带尖角)

效果图:

代码:


<com.blockmenu.liuguangli.blockmenuitem.BlockMenuItem
                android:layout_width="match_parent"
                android:background="@color/commonDivBgWhite"
                android:layout_marginTop="20dp"
                android:layout_height="62dp"
                app:mainIcon="@mipmap/icon_mine"
                app:IconMargin="15dp"
                app:mainIconSize="24dp"
                app:topBorder="0.5dp"
                app:bottomBorder="0.5dp"
                app:text="@string/butler"
                app:textSize="@dimen/common_content_text"
                app:textMargin="10dp"
                app:extendIcon="@mipmap/arrow_right_gray"
                />



这样是不是简洁多了。
2)BarGroup Style

效果图:

代码:


<com.blockmenu.liuguangli.blockmenuitem.BlockMenuItem
                android:layout_width="match_parent"
                android:background="@color/commonDivBgWhite"
                android:layout_marginTop="10dp"
                android:layout_height="62dp"
                app:mainIcon="@mipmap/icon_service"
                app:IconMargin="15dp"
                app:mainIconSize="24dp"
                app:topBorder="0.5dp"
                app:bottomBorder="0.5dp"
                app:text="@string/service"
                app:textSize="@dimen/common_content_text"
                app:textMargin="10dp"
                app:extendIcon="@mipmap/arrow_right_gray"
                app:bottomBorderStartFromText="true"
                />
  <com.blockmenu.liuguangli.blockmenuitem.BlockMenuItem
                android:layout_width="match_parent"
                android:background="@color/commonDivBgWhite"
                android:layout_height="62dp"
                app:mainIcon="@mipmap/icon_home"
                app:IconMargin="15dp"
                app:mainIconSize="24dp"
                app:bottomBorder="0.5dp"
                app:text="@string/shake_sopen_door"
                app:textSize="@dimen/common_content_text"
                app:textMargin="10dp"
                app:extendIcon="@mipmap/arrow_right_gray"
                />  


简单吧,实际上就是两个SingleBar叠在一起,然后你指定一下边界框就行了(第一个 SingleBar 的下边框指定和文本对齐,第二个 SingleBar 不设置上边框)。

3)Block Style
效果图:

代码:

<com.blockmenu.liuguangli.blockmenuitem.BlockMenuItem
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        app:mainIcon="@mipmap/icon_butler_pressed"
                        app:IconMargin="15dp"
                        app:topBorder="2dp"
                        app:text="@string/butler"
                        app:textSize="@dimen/common_content_text"
                        app:textColor="@color/common"
                        app:extendIcon="@mipmap/arrow_right_gray"
                        app:textMargin="10dp"
                        app:mainIconSize="60dp"
                        app:vertical="true"
                        />

这个使用就更简单了,细心的你一定发现了,只需多设置一个属性:app:vertical=”true”,从儿实现来图标文本的纵向排列。
4) 没有了,下载源码使用吧。

   个人不喜欢上来就说一堆技术细节,这是一个开源demo,上面简单介绍了下如何使用,至于实现知识和技术细节,我想你在体验效果之后再去研究比较符合人的认知逻辑,或许后续我可能在后面补充章节介绍自定义控件的相关知识和技术细节。
demo下载地址:
https://github.com/liuguangli/BlockIconTextMenu

目录
相关文章
|
12月前
|
存储 安全 虚拟化
虚拟化技术:实现资源高效利用和灵活管理的利器
虚拟化技术作为实现资源高效利用和灵活管理的重要手段,在数字化时代背景下,正逐步改变传统IT架构模式。本文概述了虚拟化技术的概念、原理及其在数据中心管理、云计算平台、企业信息化建设、科研教育及医疗行业的应用,并探讨了其面临的挑战与未来发展趋势。
588 3
|
编解码 数据可视化 前端开发
如何使用 D3.js 创建一个交互式的地图可视化?
如何使用 D3.js 创建一个交互式的地图可视化?
476 6
使用StringStream处理字符串
使用StringStream处理字符串
|
12月前
|
存储 机器学习/深度学习 人工智能
二维码生成原理和解码原理
二维码(Quick Response Code,简称QR码)是一种广泛使用的二维条形码技术。二维码能有效地存储和传递信息,广泛应用于商品追溯、支付、广告等多个领域。二维码的主要特点是信息存储量大、读取速度快、容错能力强等。
1759 2
|
Java Spring
快速解决Spring Boot跨域困扰:使用CORS实现无缝跨域支持
这是一个简单的配置示例,用于在Spring Boot应用程序中实现CORS支持。根据你的项目需求,你可能需要更详细的配置来限制允许的来源、方法和标头。
1321 3
|
分布式计算 Hadoop Linux
Linux配置SSH免密码登录(非root账号)
设置多台linux服务器之间以非root账号的身份免密码ssh登录
1108 0
Linux配置SSH免密码登录(非root账号)
|
索引 搜索推荐 自然语言处理
“搜索”的原理,架构,实现,实践,面试不用再怕了(值得收藏)!!!
可能99%的同学不做搜索引擎,但99%的同学一定实现过检索功能。搜索,检索,这里面到底包含哪些技术的东西,希望本文能够给大家一些启示。
1423 0
|
机器学习/深度学习 人工智能 PyTorch
Paddle 点灯人 之 Paddle介绍
首先,我想讲一下,为什么我想学习paddle,或者说为什么要学习paddle。百度已经很早就做paddle了,这可以说是国内最好的学习框架了,他可以说到现在已经非常成熟了,相对于pytorch,他的历史确实没有那么悠久,并且很多人都在用pytorch而不是paddle。不过paddle在国内来说,是比较好的,很多东西都有简单的部署,可以更好的使用,所以我也想学习paddle,也为了有更多机会。
Paddle 点灯人 之 Paddle介绍
|
JavaScript
Notification.description(ant-design) 和 $notify.message(element-ui) 通知内容自定义
Notification.description(ant-design) 和 $notify.message(element-ui) 通知内容自定义
725 0
|
编译器 Linux C语言
在C语言/C++中把资源编译进exe可执行文件,并运行时释放资源
在C语言/C++中把资源编译进exe可执行文件,并运行时释放资源
717 0