本博客主要讲以下两点知识点

图标改变颜色:Drawable的变色,让Android也能有iOS那么方便的图片色调转换,就像同一个图标,但是有多个地方使用,并且颜色不一样,就可以用这个方法了。

搜索框: 一般是EditText实现,本文 实现 TextView图片和文字居中,键盘搜索。

来看看效果图:

图标改变颜色:第一个界面的左边(二维码)和右边(更多)两个实现,我放进去的图片是黑色的,显示出来是白色的。

搜索框:第一个界面的图片和文字居中,还可以设置间距,第二个见面搜索设置键盘搜索按钮,点击搜索监听事件,清除内容的图标。

搜索框布局:

    <!--
搜索图标设置 左边
android:drawableLeft="@mipmap/icon_search"
android:drawablePadding="5dp" 图标和文字的间距
右边
android:drawableRight="@mipmap/round_close"
android:paddingRight="8dp"
android:imeOptions="actionSearch" 设置成搜索按钮
-->
<EditText
android:id="@+id/search_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="30dp"
android:hint="输入要搜索的商品"
android:background="@drawable/search_gray"
android:layout_marginTop="10dp"
android:layout_marginLeft="9dp"
android:textSize="12sp"
android:drawableLeft="@mipmap/icon_search"
android:paddingLeft="9dp"
android:drawablePadding="5dp"
android:drawableRight="@mipmap/round_close"
android:paddingRight="8dp"
android:imeOptions="actionSearch"
android:maxLines="1"
android:singleLine="true"
/>

键盘监听:

searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId == 0 || actionId == 3) && event != null) {
             //提示搜索内容
Toast.makeText(SearchActivity.this,searchText.getText().toString(),Toast.LENGTH_LONG).show();
//可以跳转搜索页面
/* Intent intent= new Intent(SearchActivity.this,SearchWebViewActivity.class);
intent.putExtra("model",model);
intent.putExtra("search",searchText.getText().toString());
startActivity(intent);
finish();*/
}
return false;
}
});

首页布局:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="45dp"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageButton
android:id="@+id/home_left_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="19dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="11dp"
android:layout_centerVertical="true"
android:background="#00000000"
/>
<com.zhangqie.searchbox.view.DrawableTextView
android:id="@+id/home_search"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_weight="1"
android:background="@drawable/search_view_background"
android:gravity="center_vertical"
android:maxLines="1"
android:text="输入搜索相关内容"
android:drawableLeft="@mipmap/icon_search"
android:textSize="12sp"
android:drawablePadding="11dp"
/>
<ImageButton
android:id="@+id/home_right_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:paddingRight="15dp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="15dp"
android:background="#00000000"
/>
</LinearLayout>

自定义DrawableTextView:(文字图标居中)

public class DrawableTextView extends TextView {

    public DrawableTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
} public DrawableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} public DrawableTextView(Context context) {
super(context);
} @Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
// 得到drawableLeft设置的drawable对象
Drawable leftDrawable = drawables[0];
if (leftDrawable != null) {
// 得到leftDrawable的宽度
int leftDrawableWidth = leftDrawable.getIntrinsicWidth();
// 得到drawable与text之间的间距
int drawablePadding = getCompoundDrawablePadding();
// 得到文本的宽度
int textWidth = (int) getPaint().measureText(getText().toString().trim());
int bodyWidth = leftDrawableWidth + drawablePadding + textWidth;
canvas.save();
canvas.translate((getWidth() - bodyWidth) / 2, 0);
}
super.onDraw(canvas);
}
}

看似简单的效果,其实还是不简单的;加油吧!  有问题可以扫头像加新创建的群@我 

 源码下载

Android----- 改变图标原有颜色 和 搜索框的更多相关文章

  1. android改变字体的颜色的三种方法

    写文字在Android模拟器中的方法 法一: main.xml配置文件: <TextView android:id="@+id/tv" android:layout_widt ...

  2. Android开发 ---代码创建选项菜单、隐藏菜单项、菜单的生命周期,菜单按钮图标设置、搜索框、xml中设置子菜单

    1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> < ...

  3. iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)

    很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...

  4. iOS搜索框

    在iOS8以前搜索框是作为一个控件添加到TableViewController中, 有系统自带的搜索变量self.searchDisplayController 遵守一个搜索显示的协议<UISe ...

  5. ThinkPHP搜索框需要注意的事项

    1.当搜索成功后需要用到分页的时候,form表单需要用get传参 2.编码方式 当编码方式不正确的时候,使用分页类改变分页,会使搜索框里面的内容乱码 改变编码方式的方法 第一种:header(&quo ...

  6. Android 指定纯色图标的颜色

        最近项目用到了系统图标,但是设计师设计的颜色却与系统图标不一样: 如果每张图片都要用Photoshop进行颜色填充势必增加了工作量,而且不灵活,占资源: 例如同一张图片,希望点击的时候改变颜色 ...

  7. Android 根据EditText搜索框ListView动态显示数据

    根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...

  8. Android actionbar 搜索框

    就是实如今顶部这种搜索框. 一.这个搜索框是actionbar上的menu上的一个item.叫SearchView.我们能够先在menu选项里定义好: bmap_menu.xml: <?xml ...

  9. Android之MaterialDesign应用技术2-仿支付宝上滑搜索框缓慢消失

    PS:在这之前也就是上一篇介绍了MaterialDesign一些滑动删除.标题栏的悬浮效果等,如果没看过第一篇的小火鸡可以看一下,因为这篇是接着上一篇写的,有一些东西上一篇写过了这里就不在重复了(Ma ...

随机推荐

  1. IBM Java 7 新特性和在 WAS 8.5 中的配置【转载】

    IBM Java 7新特性以及在WAS V8.5 中的安装与版本切换 简介: 本文介绍了 IBM Java 7 的基本新特性以及 IBM 特有的新特性,并详细的介绍和分析了 JVM 所采用的新的垃圾回 ...

  2. 随堂小测app(nabcd)

    N 现在,老师想要组织测验,需要提前印制试卷,费时费力,考勤采取传统的点名的方式,过程繁琐且结果水分大. 而随堂小测app通过在线答题,智能定位可以帮助老师掌握学生对知识的掌握程度,了解学生的到客情况 ...

  3. Python(线程进程3)

    四 协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切 ...

  4. SQL Server返回插入数据的ID和受影响的行数

    首先看看数据库里面的数据(S_Id为自增长标识列): sql server 中返回上一次插入数据的ID(标识值)有三种方式: 第一种 @@IDENTITY: insert into Student(S ...

  5. MyBatis For .NET学习-问题总结

    1. MyBatis在进行sqlserver与c# 类型转换时需要注意,sqlserver中dbtype为float时,c#需要使用double与之对应,而不能使用float或decimal 2. M ...

  6. nginx日志输出,https,ssl

    日志输出(浏览器直接访问)缺省安装下,浏览器是无法访问日志的,需要在编译的时候附带参数安装这些模块 ./configure --prefix=/usr/local/nginx --with-http_ ...

  7. tensorboard实现训练的可视化

    tensorboard是tensorflow自带的可视化工具 输入命令可以启动tensorboard服务. tensorboard --logdir=your log dir 通过浏览器localho ...

  8. python3_pickle模块详解

    python3 pickle持久化的储存数据. python程序运行中得到了一些字符串,列表,字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据.python模块大 ...

  9. vmware 虚拟机三种网卡

    转:https://blog.csdn.net/lyf_ldh/article/details/78695357 vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NA ...

  10. Heartbeats

    很少有人否定,这是一首天籁.凄美动听的声音,触动的,是真正的灵魂深处.所谓“仁者见仁智者见智”,但有些东西是共通的,比如,我们的内心会被同一样东西触动.在这首动听的歌曲中,体现出那男女之间平凡又伟大的 ...