3.Android 优化布局(解决TextView布局)
转载:http://www.jianshu.com/p/d3027acf475a
今天分享一个Layout布局中的一个小技巧,希望看过之后你也可以写出性能更好的布局,我个人的目的是用最少的view写出一样的效果布局
用TextView同时显示图片和文字:
先看一下效果图
以上这四块区域相信大家在项目中经常遇到吧!(一般的写法ImageView与TextView的组合)现在用一个自定义的TextView就完成能达到一样的效果,并且也可以设置背景选择器、图片的尺寸大小,不需要嵌套多层布局在设置相关的属性
第一块Xml中的代码
<com.~~~~~~.TextDrawable
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textdrawable"
android:clickable="true"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:padding="@dimen/space_20"
android:text="设置"
android:textColor="@color/black_252c3d"
android:textSize="@dimen/textsize_20sp_in_normal"
app:leftDrawable="@drawable/tab_more_unselect"
app:leftDrawableHeight="@dimen/space_60"
app:leftDrawableWidth="@dimen/space_60"
app:rightDrawable="@drawable/iconfont_youjiantou"
app:rightDrawableHeight="20dp"
app:rightDrawableWidth="10dp"
/>
<Space
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/gray_888888"
/>
下面的分割线我建议用Space这个控件,它是一个非常轻量级的控件
第二块Xml中的代码
<com.~~~~~~.TextDrawable
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textdrawable"
android:clickable="true"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:padding="@dimen/space_20"
android:text="消息"
android:textColor="@color/black_252c3d"
android:textSize="@dimen/textsize_20sp_in_normal"
app:leftDrawable="@drawable/tab_speech_unselect"
app:leftDrawableHeight="@dimen/space_60"
app:leftDrawableWidth="@dimen/space_60"
/>
第三块Xml中的代码
<com.~~~~~~.TextDrawable
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/textdrawable"
android:clickable="true"
android:drawablePadding="10dp"
android:gravity="center"
android:padding="@dimen/space_20"
android:text="首页"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/textsize_20sp_in_normal"
app:topDrawable="@drawable/tab_home_select"
app:topDrawableHeight="@dimen/space_60"
app:topDrawableWidth="@dimen/space_60"
/>
第四块Xml中的代码(图片按钮)
<com.hightsstudent.highsstudent.ui.widget.TextDrawable
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/textdrawable"
android:clickable="true"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:padding="@dimen/space_15"
android:text="退出"
android:textColor="@color/black_252c3d"
android:textSize="@dimen/textsize_20sp_in_normal"
app:rightDrawable="@drawable/icon_backs"
app:rightDrawableHeight="@dimen/space_80"
app:rightDrawableWidth="@dimen/space_80"
/>
下面贴出TextDrawable.java代码
/**
* Created by Dengxiao on 2016/11/8.
*/
public class TextDrawable extends TextView {
private Drawable drawableLeft;
private Drawable drawableRight;
private Drawable drawableTop;
private int leftWidth;
private int rightWidth;
private int topWidth;
private int leftHeight;
private int rightHeight;
private int topHeight;
private Context mContext;
public TextDrawable(Context context) {
this.mContext=context;
this(context, null, 0);
}
public TextDrawable(Context context, AttributeSet attrs) {
this.mContext=context;
this(context, attrs, 0);
}
public TextDrawable(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext=context;
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextDrawable);
drawableLeft = typedArray.getDrawable(R.styleable.TextDrawable_leftDrawable);
drawableRight = typedArray.getDrawable(R.styleable.TextDrawable_rightDrawable);
drawableTop = typedArray.getDrawable(R.styleable.TextDrawable_topDrawable);
if (drawableLeft != null) {
leftWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_leftDrawableWidth, dip2px(context, 20));
leftHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_leftDrawableHeight, dip2px(context, 20));
}
if (drawableRight != null) {
rightWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_rightDrawableWidth, dip2px(context, 20));
rightHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_rightDrawableHeight, dip2px(context, 20));
}
if (drawableTop != null) {
topWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_topDrawableWidth, dip2px(context, 20));
topHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_topDrawableHeight, dip2px(context, 20));
}
}
public int dip2px(Context context, float dpValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (drawableLeft != null) {
drawableLeft.setBounds(0, 0, leftWidth, leftHeight);
}
if (drawableRight != null) {
drawableRight.setBounds(0, 0, rightWidth, rightHeight);
}
if (drawableTop != null) {
drawableTop.setBounds(0, 0, topWidth, topHeight);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, null);
}
/**
* 设置左侧图片并重绘
*/
public void setDrawableLeft(Drawable drawableLeft) {
this.drawableLeft = drawableLeft;
invalidate();
}
/**
* 设置左侧图片并重绘
*/
public void setDrawableLeft(int drawableLeftRes) {
this.drawableLeft = mContext.getResources().getDrawable(drawableLeftRes);
invalidate();
}
/**
* 设置右侧图片并重绘
*/
public void setDrawableRight(Drawable drawableRight) {
this.drawableRight = drawableLeft;
invalidate();
}
/**
* 设置右侧图片并重绘
*/
public void setDrawableRight(int drawableRightRes) {
this.drawableRight = mContext.getResources().getDrawable(drawableRightRes);
invalidate();
}
/**
* 设置上部图片并重绘
*/
public void setDrawable(Drawable drawableTop) {
this.drawableTop = drawableTop;
invalidate();
}
/**
* 设置右侧图片并重绘
*/
public void setDrawableTop(int drawableTopRes) {
this.drawableTop = mContext.getResources().getDrawable(drawableTopRes);
invalidate();
}
}
附上attrs:
<declare-styleable name="TextDrawable">
<attr name="leftDrawable" format="reference"/>
<attr name="leftDrawableWidth" format="dimension"/>
<attr name="leftDrawableHeight" format="dimension"/>
<attr name="rightDrawable" format="reference"/>
<attr name="rightDrawableWidth" format="dimension"/>
<attr name="rightDrawableHeight" format="dimension"/>
<attr name="topDrawable" format="reference"/>
<attr name="topDrawableWidth" format="dimension"/>
<attr name="topDrawableHeight" format="dimension"/>
</declare-styleable>
以上为全部代码
3.Android 优化布局(解决TextView布局)的更多相关文章
- Android绘制优化(二)布局优化
前言 我们知道一个界面的测量和绘制是通过递归来完成的,减少布局的层数就会减少测量和绘制的时间,从而性能就会得到提升.当然这只是布局优化的一方面,那么如何来进行布局的分析和优化呢?本篇文章会给你一个满意 ...
- Android UI学习 - FrameLayou和布局优化(viewstub)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/308090 Fram ...
- Android性能优化之中的一个 布局优化
本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...
- Android实习生 —— 屏幕适配及布局优化
为什么要进行屏幕适配.对哪些设备进行适配?在近几年的发展当中,安卓设备数量逐渐增长,由于安卓设备的开放性,导致安卓设备的屏幕尺寸大小碎片化极为严重.从[友盟+]2016年手机生态发展报告H1中看截止1 ...
- Android——四大组件、六大布局、五大存储
一.android四大组件 (一)android四大组件详解 Android四大组件分别为activity.service.content provider.broadcast receiver. 1 ...
- Android之控件与布局,结构知识点,基础完结
版权声明:未经博主允许不得转载 在Android中我们常常用到很多UI控件,如TextView,EditText,ImageView,Button,ImageButton,ToggleButton,C ...
- Android学习系列(5)--App布局初探之简单模型
人类科技的进步源自探索,探索来自于发现本原,当然App布局没这么先进,本文也只是一个归类总结.这篇文章是Android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. Androi ...
- Android 自定义View及其在布局文件中的使用示例(三):结合Android 4.4.2_r1源码分析onMeasure过程
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3549365.html From crash_coder linguowu linguowu0622@gami ...
- Android 自定义View及其在布局文件中的使用示例(二)
转载请注明出处 http://www.cnblogs.com/crashmaker/p/3530213.html From crash_coder linguowu linguowu0622@gami ...
随机推荐
- 用django实现一个微信图灵机器人
微信的post请求格式是xml,所以django需要做的就是将xml请求解析出来,把content发送到图灵机器人接口, 接口返回的json数据把主要内容给解析出来,然后重新封装成xml返回给微信客户 ...
- PAT 1012. 数字分类 (20)
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: ...
- LOG4NET日志配置及使用
Log4net的安装 Install-Package log4net 1.先弄个日志记录的类 /// <summary> /// 使用LOG4NET记录日志的功能,在WEB.CONFIG里 ...
- Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...
- 50个查询系列-第13个查询:把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
UPDATE tblscore SET tblscore.Score= ( -- 这里开始算叶平的平均值 SELECT AVG(tt.aa) FROM ( SELECT tblscore.Score ...
- 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块
出于兴趣爱好,前段时间自己尝试写了一个叫simple的cms,里面使用了百度ueditor编辑器,发现它的多图片上传模块很不错,用起来很方便,又可以选择已经上传好的图片.正好我又是个懒人,发现有现成的 ...
- 安装 SQL SERVER 2008 必须使用 "角色管理工具" 错误 的 解决方案 (转)
刚在服务器(Win2008)上安装SqlServer2008的时候出现了这么一个报错——必须使用“角色管理工具”安装或配置Microsoft .NET Framework 3.5 SP1.一开始以为是 ...
- 极简Word排版示例(以Word2013为例)
文档标题 第一行写下文档的名字,居中,微软雅黑字体,三号 章节标题 每一章的标题单独一行,光标选中这行,设置为标题1 每一节的标题单独一行,光标选中这行,设置为标题2 全部章节标题设置完毕后,下一步 ...
- GWT-Dev-Plugin(即google web toolkit developer plugin)for firefox的下载地址
如果FireFox的版本为20,则对应google-web-toolkit的插件离线下载地址,不要用浏览器直接下载,用Flashget等客户端下载,超快. http://google-web-tool ...
- vijos P1009清帝之惑之康熙
</pre>背景康熙是中国历史乃至世界历史中最伟大的帝王之一,清除螯拜,撤除三藩,统一台湾,平定准葛尔叛乱:与此同时,出众的他也被世界各国遣清使臣所折服.康熙是历史上少有的全人,不仅文武兼 ...