星空网 > 软件开发 > 操作系统

Android之UI

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

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

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

本例的运行截图:

Android之UIimages/loading.gif' data-original="http://images2015.cnblogs.com/blog/658506/201605/658506-20160518133323873-884060871.png" />Android之UI

具体的实现代码如下:

MainActivity

package com.example.testimport 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.test 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 }

 1 <LinearLayout  2    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   <Button10     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.test19       android:layout_width="match_parent"20       android:layout_height="wrap_content"21       android:paddingLeft="100dp"22       />23   24 25 </LinearLayout>

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




原标题:Android之UI

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

货代泰国:https://www.goluckyvip.com/tag/89144.html
泰国寄过来的快递一般到中国哪里:https://www.goluckyvip.com/tag/89145.html
泰来寄到泰国快递:https://www.goluckyvip.com/tag/89146.html
泰国的货代:https://www.goluckyvip.com/tag/89147.html
货运泰国:https://www.goluckyvip.com/tag/89148.html
中国寄东西到泰国怎么寄:https://www.goluckyvip.com/tag/89149.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
石象湖景区门票-石象湖景区门票优惠政策:https://www.vstour.cn/a/411243.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流