切换两个控件之间的焦点,TV、机顶盒用android自带的控件 例如listview或者gridview 是,因为需要适配手机等,所以控件某些方面(如每个控件的焦点,切换时的速度,位置等)是不会很匹配TV、机顶盒的.最好的方法是自己绘制一个gridview

正常的GridView,然而item的控件就需要自定义了:
MyImageView

import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Color;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.ImageView;@SuppressLint("AppCompatCustomView")public class MyImageView extends ImageView {    public MyImageView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public MyImageView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyImageView(Context context) {        super(context);    }    @Override    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);        if (gainFocus) {            setBackgroundColor(Color.rgb(255, 138, 0));        } else {            // 此处为去掉控件高亮显示            setBackgroundColor(0);        }    }}

MyTextView

import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.KeyEvent;import android.widget.TextView;@SuppressLint("AppCompatCustomView")public class MyTextView extends TextView {    private static final String TAG = "textview";    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }    @Override    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {        // TODO Auto-generated method stub        super.onFocusChanged(focused, direction, previouslyFocusedRect);        // 此处为该控件获得焦点后高亮显示        if (focused) {            setBackgroundColor(Color.rgb(255, 138, 0));        } else {            // 此处为去掉控件高亮显示            setBackgroundColor(0);        }    }    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        Log.d("onkeydown", "onkeydown");        return super.onKeyDown(keyCode, event);    }    public MyTextView(Context context) {        super(context);    }    public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }}

item引用

<?xml version="1.0" encoding="utf-8"?>            

更多相关文章

  1. Android中clipChildren属性的用法
  2. android常用控件(三)- ProgressBar、ListView
  3. Android问题集(七)——TableLayout 中让TableRow中的控件填充满整
  4. 工作总结|Android(安卓)自定义列表刷新控件
  5. 解决NestedScrollView中嵌套ViewPager,ViewPager内容无法正常显示
  6. Android(安卓)事件传递机制实测
  7. 【Android】自定义控件实现可滑动的开关(switch)
  8. Android之PopupWindow弹出对话框
  9. Android(安卓)控件之Spinner

随机推荐

  1. Android插件开发初探——分析篇
  2. Android Studio 单元测试
  3. android wifi 连接笔记
  4. Android学习笔记(38):Handler消息传递处
  5. Android在MediaMuxer和MediaCodec录制视
  6. Android 开发使用 Gradle 配置构建库模块
  7. Activity的launchMode相关
  8. 购买Android应用
  9. Android新手入门2016(10)--GridView
  10. Android的一些冷知识