在本文中实现的是比较普遍的一个对EditText的重绘以及对于按钮或窗口添加渐变色。

  因为EditText是继承于TextView的,所以可以实现对EditText的重绘,在重绘的时候只需要继承EditText并且重写它的onDraw()方法就可以了。

  在给按钮或者窗口添加渐变色的时候需要借用GradientDrawable方法设置渐变的方向,以及渐变的颜色,将渐变的颜色放在一个数组中然后对其进行访问。并且使用setBackgroundDrawable()方法将其显示在界面上。

本例的运行截图:

具体的实现代码如下:

MainActivity

package com.example.testxml;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private Button btn;
private EditText edtxt;
// private DrawEdit drawedtxt; @SuppressLint("WrongCall")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn = (Button)this.findViewById(R.id.btncenter); //对按钮设置至上而下的渐变色
GradientDrawable gradientdrawable = new GradientDrawable(Orientation.LEFT_RIGHT,new int[]
{Color.RED,Color.BLACK,Color.YELLOW});
//设置当前窗口的渐变背景色
// getWindow().setBackgroundDrawable(gradientdrawable);
btn.setBackgroundDrawable(gradientdrawable);
} }

对EditText进行重绘 DrawEdit代码如下

 package com.example.testxml;

 import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.EditText; public class DrawEdit extends EditText{ //实现DrawEdit的构造函数
public DrawEdit(Context context) {
super(context);
} public DrawEdit(Context context, AttributeSet attrs) {
super(context, attrs);
} protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setTextSize(18);
paint.setColor(Color.GREEN);
//绘制文本
canvas.drawText("绘制文本", 2, getHeight() / 2 + 5, paint); } }

xml布局文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <Button
android:id="@+id/btncenter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/btncenter" /> <!-- 将EditText进行重绘时要对其进行自定义,这样在MainActivity中无需进行调用,他会自己调用 --> <com.example.testxml.DrawEdit
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="100dp"
/> </LinearLayout>

上面就实现了对于EditText的重绘以及对于按钮的设置的渐变色的过程。

Android之UI--重绘EditText以及实现Button的渐变色的更多相关文章

  1. Android视图状态及重绘流程分析,带你一步步深入了解View(三)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17045157 在前面一篇文章中,我带着大家一起从源码的层面上分析了视图的绘制流程, ...

  2. Android View的重绘过程之WindowManager的addView方法

    博客首页:http://www.cnblogs.com/kezhuang/p/ 关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下 <[An ...

  3. Android View的重绘过程之Draw

    博客首页:http://www.cnblogs.com/kezhuang/p/ View绘制的三部曲,测量,布局,绘画现在我们分析绘画部分测量和布局 在前两篇文章中已经分析过了.不了解的可以去我的博客 ...

  4. Android View的重绘过程之Measure

    博客首页:http://www.cnblogs.com/kezhuang/p/ View绘制的三部曲,  测量,布局,绘画今天我们分析测量过程 view的测量是从ViewRootImpl发起的,Vie ...

  5. Android View的重绘ViewRootImpl的setView方法

    博客首页:http://www.cnblogs.com/kezhuang/p/ 本篇文章来分析一下WindowManager的后续工作,也就是ViewRootImpl的setView函数的工作 /i* ...

  6. Android View的重绘过程之Layout

    博客首页:http://www.cnblogs.com/kezhuang/p/ View绘制的三部曲,测量,布局,绘画现在我们分析布局部分测量部分在上篇文章中已经分析过了.不了解的可以去我的博客里找一 ...

  7. Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性

    1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...

  8. 《Android内核剖析》读书笔记 第13章 View工作原理【View重绘过程】

    计算视图大小的过程(Measure) 视图大小,准确的来说应该是指视图的布局大小:我们在layout.xml中为每个UI控件设置的layout_width/layout_height两个属性被用来设置 ...

  9. Android深入研究Adapter重绘

    一直以来Adapter的使用都仅仅是流于表面,仅仅知道要实现几个抽象的方法,把Adapter设置给某种listView,就能够非常好的工作起来.所谓理解仅仅是建立在主观的猜想上面,认为应该是这样,对, ...

随机推荐

  1. windows和linux间互传文件

    方法1:Xshell传输文件 用rz,sz命令在xshell传输文件 很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz 首先你的Ubuntu需要安 ...

  2. alibaba笔试1

    5.D 一个线程不可以改变另一个线程的程序计数器.如果改变了,线程在切换后就恢复不到正确的位置. 一个线程可以访问另一个线程的栈.http://bbs.csdn.net/topics/39008942 ...

  3. eclipse 远程wifi调试android程序

    [原文]http://leanote.com/blog/view/541f8b2dbda4e44f75000000 下载wifidebug http://pan.baidu.com/s/1i3stnF ...

  4. JDK1.5新特性(六)……Generics

    概述 Generics - This long-awaited enhancement to the type system allows a type or method to operate on ...

  5. MVC5中使用KinEditor

    参考:http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html 第一步 将 KindEditor 的源文件添加到项目中,建议放到 / ...

  6. 广州大学华软软件学院——NA视频下载

    准备工具: 360极速浏览器(不要认错图标了): 浏览器视频下载插件: 第一步:安装浏览器插件 1.打开浏览器 2.解压,找到插件文件: 3.把插件拖到浏览器中: 4.添加,然后就完成了插件安装 查看 ...

  7. 洛谷 P1169 [ZJOI2007]棋盘制作

    2016-05-31 14:56:17 题目链接: 洛谷 P1169 [ZJOI2007]棋盘制作 题目大意: 给定一块矩形,求出满足棋盘式黑白间隔的最大矩形大小和最大正方形大小 解法: 神犇王知昆的 ...

  8. dataStructure@ Find if there is a path between two vertices in a directed graph

    Given a Directed Graph and two vertices in it, check whether there is a path from the first given ve ...

  9. HDU-4686 Arc of Dream 构造矩阵

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 因为ai = ai-1*AX+AY ,bi = bi-1*BX+BY ,那么ai*bi=AX*B ...

  10. hdoj 2023 求平均成绩

    求平均成绩 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...