Android(安卓)TV,机顶盒Gridview 焦点
16lz
2021-01-24
切换两个控件之间的焦点,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"?>
更多相关文章
- Android中clipChildren属性的用法
- android常用控件(三)- ProgressBar、ListView
- Android问题集(七)——TableLayout 中让TableRow中的控件填充满整
- 工作总结|Android(安卓)自定义列表刷新控件
- 解决NestedScrollView中嵌套ViewPager,ViewPager内容无法正常显示
- Android(安卓)事件传递机制实测
- 【Android】自定义控件实现可滑动的开关(switch)
- Android之PopupWindow弹出对话框
- Android(安卓)控件之Spinner