如何自定义FloatingActionButton的大小
Google最近为了让开发者更好的更规范的应用Material Design设计思想,特意放出了android support design library,里面含有更多Material Design的标志性组件,其中最常用的就是那个圆形按钮,叫做Floating Action Button,可以简称为FAB。一个使用该控件的例子为:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="25dp"
android:layout_gravity="start|top"
app:borderWidth="0dp"
app:fabSize="normal"
app:elevation="4dp"
android:scaleType="center"
android:src="@drawable/btn_menu" />
注意,在跟布局中需要配置app这个命令空间:
xmlns:app="http://schemas.android.com/apk/res-auto"
这里关注FAB的app:fabSize这个属性,它是用来规定大小,但是发现只有两个值:mini和normal,默认情况下官方推荐的宽高是56dp,但是如果设计师需要你显示一个比较大一些的FAB,比如100dp(这里发现将fabSize设置成normal或者mini都很小),该怎么办?
第一反应是修改layout_height和layout_width这两个属性,将他们设置成100dp,最后却发现成了这个怂样:
看来改变上述两个属性是不行的,那么我们来看FloatingActionButton的源码,在它的构造函数中,有下列代码:
ShadowViewDelegate delegate = new ShadowViewDelegate() {
public float getRadius() {
return (float)FloatingActionButton.this.getSizeDimension() / 2.0F;
} public void setShadowPadding(int left, int top, int right, int bottom) {
FloatingActionButton.this.mShadowPadding.set(left, top, right, bottom);
FloatingActionButton.this.setPadding(left + FloatingActionButton.this.mContentPadding, top + FloatingActionButton.this.mContentPadding, right + FloatingActionButton.this.mContentPadding, bottom + FloatingActionButton.this.mContentPadding);
} public void setBackgroundDrawable(Drawable background) {
FloatingActionButton.super.setBackgroundDrawable(background);
}
};
if(VERSION.SDK_INT >= 21) {
this.mImpl = new FloatingActionButtonLollipop(this, delegate);
} else {
this.mImpl = new FloatingActionButtonEclairMr1(this, delegate);
}
这里的mImpl是一个FloatingActionButtonImpl的实现类,FloatingActionButton大部分的操作实际上都是在操作这个mImpl,这个mImpl在被new出来的时候会判断当前环境是不是API level大于等于21(实际上在小于21的时候,他用drawable绘制了一层阴影从而有类似于21以上的evevation的效果),随后创建mImpl的时候传入了delegate这个参数,ShadowViewDelegate实际上就是个drawable的包装类。我们看getRadius这个实现方法,显然它是计算半径的,所以要想定制FAB的宽高,得要从这里找文章。
具体看getSizeDimension这个方法:
final int getSizeDimension() {
switch(this.mSize) {
case 0:
default:
return this.getResources().getDimensionPixelSize(dimen.fab_size_normal);
case 1:
return this.getResources().getDimensionPixelSize(dimen.fab_size_mini);
}
}
原来app:fabSize实际上是在调用fab_size_normal和fab_size_mini这两个dimen的值,相应的对应了normal和mini这两种情况,那么我们可以想到在我们的代码中国同样定义这两个dimen值对原来的进行覆盖,于是我们在醒目代码的dimen.xml中定义:
<resources>
<dimen name="fab_size_normal">100dp</dimen>
</resources>
这个时候再看效果:
修改成功
如何自定义FloatingActionButton的大小的更多相关文章
- C# 将Excel转为PDF时自定义表格纸张大小
通过后端程序将Excel表格转为PDF格式时,直接转换后的PDF效果可能出现表格页面过小或者过大,导致页面内容分布不均.要改善转换后的文档效果,只需在转换前自定义表格纸张大小,即可调整转换后的PDF页 ...
- Sublime Text3自定义全部字体大小、字体类型和背景颜色
一.定义侧栏的背景颜色.字体大小和间距 Sublime Text3的Afterglow主题链接为:https://github.com/YabataDesign/afterglow-theme 1.按 ...
- [Selenium] 最大化或自定义浏览器的大小
driver.manage().window().maximize(); //将浏览器设置为最大化的状态 driver.manage().window().setSize(new Dimens ...
- android自定义Activity窗体大小
先给大家看图吧: 看,是不是很酷呢,呵呵. 这里我说关键的地方,就是自定义Activity的窗体大小. 这个登录框它不是一个Dialog,而是一个Activity. 如何定义,即把Activity的主 ...
- C#中使用自定义的纸张大小
using System.Drawing.Printing; using System.Drawing; private void Test() { PrintDocument m_pdoc = ne ...
- 旋转屏幕时,假如自定义的xib大小变了,可能是这个属性没有修改
虽然xib内部启用了自动布局,但是当xib放入外界,xib自身的autoresizing是存在的
- php 图像裁剪(自定义裁剪图片大小)
<?php /** * 图像裁剪 * @param $title string 原图路径 * @param $content string 需要裁剪的宽 * @param $encode str ...
- Material Design 组件之 FloatingActionButton
Material Design 设计规范在 Google I/O 2014 推出,这种设计理念一经推出就受到广大开发者的喜爱,主要侧重于纸墨化创作和突出设计的实体感,使得设计更接近于真实世界,力求平滑 ...
- 关于FloatingActionButton
由于FloatingActionButton本质上是ImageView,跟ImageView相关的就不介绍,这里重点介绍新加的几个属性. app:fabSize:FloatingActionButto ...
随机推荐
- Effective Java 52 Refer to objects by their interfaces
Principle If appropriate interface types exist, then parameters, return values, variables, and field ...
- ASP.NET Session的共享
注: 在ashx文件中使用Session 首先添加引用 using System.Web.SessionState; 实现接口 public class XXXX: IHttpHandler ==&g ...
- Struts2 Spring Hibernate等各个版本下载推荐
推荐jar包下载地址: http://mvnrepository.com/ 应有尽有
- 了解linux内存管理机制(转)
今天了解了下linux内存管理机制,在这里记录下,原文在这里http://ixdba.blog.51cto.com/2895551/541355 根据自己的理解画了张图: 下面是转载的内容: 一 物理 ...
- thumbnailator图片处理
一.简介 thumbnailator是一个用来对图片对象进行操作的Java类库.通过它我们可以很方面的使用代码的方式,对图片进行一些操作.如缩放,裁减,旋转,水印等.thumbnailator项目主页 ...
- hdu 3064 twoNumber
twoNumber Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 2048/1024 K (Java/Others) Total Su ...
- centos 下使用locate命令
首先安装mlocate yum -y install mlocate 更新数据库:updatedb 查找:locate nginx
- httpclient访问网站时设置Accept-Encoding为gzip,deflate返回的结果为乱码的问题
近期迷恋上httpclient模拟各种网站登陆,浏览器中的开发者工具中查看请求头信息,然后照葫芦画瓢写到httpclient的请求中去,requestheader中有这么一段设置: Accept-En ...
- Learning C Struct
为什么需要结构体类型? 一种语言本身往往会提供一些最基本的数据类型,比如数字型(int,bigint,float,double等),字符型,日期型,布尔型等.但现实世界中,我们面对的对象总是非常复常, ...
- NOIP2009普及组细胞分裂(数论)——yhx
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...