Android之UI--重繪EditText以及實現Button的漸變色

在本文中實現的是比較廣泛的一個對EditText的重繪以及對於按鈕或窗口添加漸變色。android

  由於EditText是繼承於TextView的,因此能夠實現對EditText的重繪,在重繪的時候只須要繼承EditText而且重寫它的onDraw()方法就能夠了。canvas

  在給按鈕或者窗口添加漸變色的時候須要借用GradientDrawable方法設置漸變的方向,以及漸變的顏色,將漸變的顏色放在一個數組中而後對其進行訪問。而且使用setBackgroundDrawable()方法將其顯示在界面上。數組

本例的運行截圖:app

具體的實現代碼以下:ide

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代碼以下佈局

 1 package com.example.testxml;
 2 
 3 import android.content.Context;
 4 import android.graphics.Canvas;
 5 import android.graphics.Color;
 6 import android.graphics.Paint;
 7 import android.util.AttributeSet;
 8 import android.widget.EditText;
 9 
10 public class DrawEdit extends EditText{
11 
12     //實現DrawEdit的構造函數
13     public DrawEdit(Context context) {
14         super(context);
15     }
16 
17     public DrawEdit(Context context, AttributeSet attrs) {
18         super(context, attrs);
19     }
20 
21     protected void onDraw(Canvas canvas) {
22         super.onDraw(canvas);
23         Paint paint = new Paint();
24         paint.setTextSize(18);
25         paint.setColor(Color.GREEN);
26         //繪製文本
27         canvas.drawText("繪製文本", 2, getHeight() / 2 + 5, paint);
28         
29     }
30 
31 
32 }

xml佈局文件:this

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context=".MainActivity" >
 8     
 9     <Button
10         android:id="@+id/btncenter"
11         android:layout_width="fill_parent"
12         android:layout_height="wrap_content"
13         android:layout_marginTop="20dp"
14         android:text="@string/btncenter" />
15 
16     <!-- 將EditText進行重繪時要對其進行自定義,這樣在MainActivity中無需進行調用,他會本身調用 -->
17         
18         <com.example.testxml.DrawEdit
19             android:layout_width="match_parent"
20             android:layout_height="wrap_content"
21             android:paddingLeft="100dp"
22              />
23     
24 
25 </LinearLayout>

上面就實現了對於EditText的重繪以及對於按鈕的設置的漸變色的過程。spa

相關文章
相關標籤/搜索