AndroidTV开发中TextView、Button、Recyclerview实现焦点效果很简单,设置一个badkground就可以了,shape里面设置两种状态,获取焦点时的图片或者背景,没有焦点时的图片或背景,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_vip_focus_bg" android:state_selected="true"/>
<item android:drawable="@drawable/shape_vip_focus_bg" android:state_focused="true" />
<item android:drawable="@drawable/shape_vip_focus_bg" android:state_pressed="true" />
<item android:drawable="@color/transparent"/>
</selector>
1.获取焦点时的效果:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:color="@color/colorPrimary" android:width="4dp"/>
</shape>
2.但是ImageView设置焦点效果时会出现一个问题,由于ImageView有一个src属性,所以在设置background会被遮挡,这时不是focus无效,也不是焦点效果无用,网上的一般的方法是让UI切一张带效果的图,本文这里想了一个折中的办法,用一个父布局设置一个背景,然后把图片放到这个父布局中,图片的大小根据父布局来计算或者固定写死都行,具体看需求,代码如下:

3.完整布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="40dp"
android:background="@drawable/shape_vip_select_bg"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="文本"
android:textColor="@color/colorPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="@drawable/shape_vip_select_bg"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="按钮"
android:textColor="@color/colorPrimary"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/shape_vip_select_bg"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:scaleType="fitXY"
android:src="@mipmap/index_play_3"
android:layout_centerInParent="true"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4.实现的效果如下:

5.代码很简单,但是这个过程很痛苦,找了很多资料和第三方库,由于时间问题和局限性,所以没有采取第三方,ui也比较忙,想了这么一个笨办法,暂时先这样吧!下一篇讲解实现ImageView待阴影特性的焦点效果,也是用shape实现的.如果小伙伴们有更好地方法可以给我留言,我会积极采纳.
最后例子完整地址如下:
本文详细介绍了在Android TV开发中,如何为TextView、Button及RecyclerView等控件设置焦点效果,并针对ImageView焦点效果实现中遇到的问题提供了解决方案。通过使用shape定义不同状态下的背景,实现了获取焦点时的视觉反馈。
1万+

被折叠的 条评论
为什么被折叠?



