微信顶部导航栏字体颜色

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信顶部导航栏字体颜色相关的知识,希望对你有一定的参考价值。

底部主要分为图标的渐变,字体颜色的渐变。
图标的颜色的渐变:主要是通过canvas绘制两个不同的图片,控制其图片的alpha透明度,来达到图标的渐变。
字体颜色:字体颜色就很好说了,Animator动画框架应该很熟悉了,在Animator框架中,有一个TypeEven是来计算十六进制色值的,我们可以通过Animator来控制颜色的渐变。

通过代码来说明:
图标:底部一共是有八个图标,一个空白状态,一个是带颜色的状态。
关键代码:
bitmapPaint.setAlpha(255 - currentAlpha );
canvas.drawBitmap(normalBitmap, srcRect , dstRect , bitmapPaint);
bitmapPaint.setAlpha(currentAlpha );
canvas.drawBitmap(pitchBitmap, srcRect , dstRect , bitmapPaint);

没错,就是两个图片同时画,一个画的透明度高 一个低。 不知道微信真正的实现方式是啥样的。哈哈……

再说文字的实现:
既然都把图片给画出来了,就顺带把文字也给画了吧,很简单的代码:
if (text != null )
textPaint.setColor(textColor);
canvas.drawText(text, textX, textY, textPaint);


多简单。然而字体颜色的变化那,就是通过textColor的变化,在每次绘制的时候,动画改变
Animator,属性动画,android4.0之后的属性动画是很强大的,任何的动画都可以很方便的做出来。
通过colorAnimator来控制色值的渐变,关键点: colorAnimator.setEvaluator(new ArgbEvaluator());
要告诉Animator这个是色值的渐变,要通过TypeEventor来控制变化。
动画执行之后,不要急着去start,这里面是没有start的。
参考技术A
"window":
"navigationBarBackgroundColor": "#ffffff",
"navigationBarTextStyle": "black",
"navigationBarTitleText": "微信接口功能演示",
"backgroundColor": "#eeeeee",
"backgroundTextStyle": "light"

微信小程序自定义导航栏兼容适配所有机型

参考技术A <blockquote><p>目前小程序已在前端占了一席之地,最近公司项目上用的就是小程序开发,由于功能及页面不是很多,所以直接原生开发,毕竟坑可能会少点,在开发过程中,小程序自带导航栏和客户的设计稿导航栏排在一起,感觉很别扭,因此要求去掉微信的自带导航栏,微信提供了这方面的api,接下来我们就实操。</p></blockquote><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-c85b39b2671d31b7.jpeg " img-data=""format":"jpeg","size":14897,"height":141,"width":963" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p><span style="font-size:15px">这是小程序官方文档截图,可以看到导航栏样式支持两种,默认是带导航栏,另外一种是自定义导航栏-custom,如果使用自定义导航栏,我们可以</span></p><p><strong><span style="font-size:15px">全局配置</span></strong><span style="font-size:15px"/></p><span style="font-size:inherit">//app.json</span>
<span style="font-size:inherit">"window"</span>:
<span style="font-size:inherit">"navigationStyle"</span>: <span style="font-size:inherit">"custom"</span>

<p><strong><span style="font-size:15px">单页面配置</span></strong></p><span style="font-size:inherit">//page.json</span>

<span style="font-size:inherit">"navigationStyle"</span>: <span style="font-size:inherit">"custom"</span>

<p><strong><span style="font-size:15px">效果对比</span></strong>
</p><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-1d120c002c6ef37c.jpeg " img-data=""format":"jpeg","size":17446,"height":352,"width":922" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><span style="font-size:15px">能明显的看出来,自定义导航栏页面内容已经顶到屏幕顶端,除了胶囊按钮,其他都是页面可控区域。</span><span style="font-size:15px">每个手机的屏幕都不一样,各家系统的状态栏高度也不一样,因此,我们在开发页面时要考虑屏幕的适配,有刘海的,要留出刘海的距离,没有的,要把状态栏高度留出来。</span><p><strong><span style="font-size:16px">1.获取导航栏高度及按钮位置</span></strong>
</p><p><span style="font-size:15px">微信提供了获取导航栏高度的Api和胶囊按钮位置的Api</span>
</p><span style="font-size:inherit">// 系统信息</span>
<span style="font-size:inherit">const</span> systemInfo = wx.getSystemInfoSync();
<span style="font-size:inherit">// 胶囊按钮位置信息</span>
<span style="font-size:inherit">const</span> menuButtonInfo = wx.getMenuButtonBoundingClientRect();
<p><span style="font-size:15px">在控制台打印出这两个Api返回结果</span></p><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-7925dd39fb0ec47a.jpeg " img-data=""format":"jpeg","size":39744,"height":497,"width":679" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p><span style="font-size:15px">这里面我们只说几个我们接下来用到的参数。</span></p>statusBarHeight <span style="font-size:inherit">// 状态栏高度</span>
screenWidth <span style="font-size:inherit">// 胶囊的宽度</span>

top <span style="font-size:inherit">// 胶囊到顶部距离</span>
height <span style="font-size:inherit">// 胶囊的高度</span>
right <span style="font-size:inherit">// 胶囊距离右边的距离</span>
<p><span style="font-size:15px">通过这几个参数,我们可以计算出状态栏的高度,微信胶囊所占的高度(存在padding值,可以使元素和胶囊纵向居中)</span>
</p><p><span style="font-size:15px">首先在app.js中定义全局data-globalData</span></p>globalData:
navBarHeight: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 导航栏高度</span>
menuBotton: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 胶囊距底部间距(保持底部间距一致)</span>
menuRight: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 胶囊距右方间距(方保持左、右间距一致)</span>
menuHeight: <span style="font-size:inherit">0</span>, <span style="font-size:inherit">// 胶囊高度(自定义内容可与胶囊高度保证一致)</span>
,
<p><span style="font-size:15px">新建个方法</span>
</p>setNavBarInfo()
<span style="font-size:inherit">// 获取系统信息</span>
const systemInfo = wx.getSystemInfoSync();
<span style="font-size:inherit">// 胶囊按钮位置信息</span>
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
<span style="font-size:inherit">// 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度</span>
<span style="font-size:inherit">this</span>.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * <span style="font-size:inherit">2</span> + menuButtonInfo.height + systemInfo.statusBarHeight;
<span style="font-size:inherit">this</span>.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
<span style="font-size:inherit">this</span>.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
<span style="font-size:inherit">this</span>.globalData.menuHeight = menuButtonInfo.right;

<p><span style="font-size:15px">在onLaunch中调用,因为我这个项目是所有的导航都不用微信自带的,所以在app.js</span>
</p><p><span style="font-size:15px">中调用及设置data。</span></p> onLaunch()
<span style="font-size:inherit">this</span>.setNavBarInfo()
,
<p><span style="font-size:15px"/><span style="font-size:15px">到这里所需要用到的都已经存了起来,页面用法也比较简单,排除状态栏的高度,设置导航栏的高度和胶囊高度保持,用flex布局。</span>
</p><p><strong><span style="font-size:16px">2.页面适配</span></strong></p><p><span style="font-size:15px">首先page.js中定义变量</span></p><span style="font-size:inherit">var</span> app = getApp()
Page(
<span style="font-size:inherit">/*
* 页面的初始数据
/</span>
data:
navBarHeight: app.globalData.navBarHeight, <span style="font-size:inherit">//导航栏高度</span>
menuBotton: app.globalData.menuBotton, <span style="font-size:inherit">//导航栏距离顶部距离</span>
menuRight: app.globalData.menuRight, <span style="font-size:inherit">//导航栏距离右侧距离</span>
menuHeight: app.globalData.menuHeight, <span style="font-size:inherit">//导航栏高度</span>

)
<p><span style="font-size:15px">页面使用</span>
</p><span style="font-size:inherit"><<span style="font-size:inherit">view</span> <span style="font-size:inherit">class</span>=<span style="font-size:inherit">"nav"</span> <span style="font-size:inherit">style</span>=<span style="font-size:inherit">"height:navBarHeightpx;"</span>></span>
<span style="font-size:inherit"><<span style="font-size:inherit">view</span> <span style="font-size:inherit">class</span>=<span style="font-size:inherit">"nav-main"</span>></span>
<span style="font-size:inherit"></span>
<span style="font-size:inherit"><<span style="font-size:inherit">view</span>
<span style="font-size:inherit">class</span>=<span style="font-size:inherit">"capsule-box"</span> <span style="font-size:inherit">style</span>=<span style="font-size:inherit">"style="</span><span style="font-size:inherit">height:</span>menuHeight+menuBotton 2<span style="font-size:inherit">px</span>; <span style="font-size:inherit">min-height:</span>menuHeight<span style="font-size:inherit">px</span>; <span style="font-size:inherit">line-height:</span>menuHeight<span style="font-size:inherit">px</span>; <span style="font-size:inherit">bottom:0px</span>;<span style="font-size:inherit">padding:0</span> menuRight<span style="font-size:inherit">px</span>;"></span>
<span style="font-size:inherit"></span>
<span style="font-size:inherit"><<span style="font-size:inherit">slot</span>></span><span style="font-size:inherit"></<span style="font-size:inherit">slot</span>></span>
<span style="font-size:inherit"></<span style="font-size:inherit">view</span>></span>
<span style="font-size:inherit"></<span style="font-size:inherit">view</span>></span>
<span style="font-size:inherit"></<span style="font-size:inherit">view</span>></span>
<p><span style="font-size:15px">wxss</span>
</p><span style="font-size:inherit">/ 公共导航 */</span>
<span style="font-size:inherit">.nav</span>
<span style="font-size:inherit">position</span>: fixed;
<span style="font-size:inherit">top</span>: <span style="font-size:inherit">0</span>;
<span style="font-size:inherit">left</span>: <span style="font-size:inherit">0</span>;
<span style="font-size:inherit">box-sizing</span>: border-box;
<span style="font-size:inherit">width</span>: <span style="font-size:inherit">100vw</span>;
<span style="font-size:inherit">z-index</span>: <span style="font-size:inherit">1000</span>;

<span style="font-size:inherit">.nav-main</span>
<span style="font-size:inherit">width</span>: <span style="font-size:inherit">100%</span>;
<span style="font-size:inherit">height</span>: <span style="font-size:inherit">100%</span>;
<span style="font-size:inherit">box-sizing</span>: border-box;
<span style="font-size:inherit">position</span>: relative;

<span style="font-size:inherit">.nav</span> <span style="font-size:inherit">.capsule-box</span>
<span style="font-size:inherit">position</span>: absolute;
<span style="font-size:inherit">box-sizing</span>: border-box;
<span style="font-size:inherit">width</span>: <span style="font-size:inherit">100%</span>;
<span style="font-size:inherit">display</span>: flex;
<span style="font-size:inherit">align-items</span>: center;

<p><span style="font-size:15px">最终效果</span></p><div class="image-package"><img src=" https://upload-images.jianshu.io/upload_images/8877503-ef017724a144e58c.jpeg " img-data=""format":"jpeg","size":4933,"height":105,"width":349" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><p><span style="font-size:15px"/>
</p><p><span style="font-size:15px">此种适配方案适应所有手机,应该说是最优的选择。</span></p>

以上是关于微信顶部导航栏字体颜色的主要内容,如果未能解决你的问题,请参考以下文章

如何更改导航栏后退按钮的字体和颜色

iOS更改导航栏标题字体和颜色

iOS中设置导航栏标题的字体颜色和大小

iOS开发-设置导航栏title字体颜色

iOS开发-设置导航栏title字体颜色

IOS设置导航栏字体大小及颜色