重构Button类,实现APP在大屏幕下用遥控器遥控,界面按钮在获取焦点下显示边框

来源:互联网 发布:mysql sql union 编辑:程序博客网 时间:2024/04/28 08:52

以下为MyButton类:

package com.TouchLife.touchlife;import android.content.Context;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;public class MyButton extends android.widget.Button{public MyButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public MyButton(Context context) {super(context);// TODO Auto-generated constructor stub}public MyButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}    private boolean focused;@Overrideprotected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {super.onFocusChanged(focused, direction, previouslyFocusedRect);this.focused = focused;if(focused){android.view.ViewGroup.LayoutParams temp = this.getLayoutParams();temp.height = this.getHeight() - 1;temp.width = this.getWidth() - 1;this.setLayoutParams(temp);}else{android.view.ViewGroup.LayoutParams temp = this.getLayoutParams();temp.height = this.getHeight() + 1;temp.width = this.getWidth() + 1;this.setLayoutParams(temp);}}@Overrideprotected void onDraw(android.graphics.Canvas canvas) {//this.getbaif(focused){Paint paint = new Paint();paint.setColor(0xeeFAC23D);canvas.drawLine(1, 1, this.getWidth()-1, 1, paint);canvas.drawLine(1, 1, 1, this.getHeight()-1, paint);canvas.drawLine(1, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);canvas.drawLine(this.getWidth()-1, 1, this.getWidth()-1, this.getHeight()-1, paint);}super.onDraw(canvas);}}

使用方法:

xml布局文件直接引用:




0 0
原创粉丝点击