Android: change style when focused

旧街凉风 提交于 2019-12-22 10:39:36

问题


I have this custom layout:

- LinearLayout
    - FrameLayout
        - ImageView
    - TextView

This layout reacts to click events (using LinearLayout.setOnClickListener()) and is made focusable using android:focusable="true" in the layout XML file. This all works fine if you're using the touchscreen, but I'm thinking about users who don't use the touchscreen that much and prefer navigation keys or maybe even don't have a touchscreen. These users won't be able to see when that ViewGroup is focused (although it can be focused using the keyboard).

My question is: how can I make a change in that layout when it is focused (I need to change the android:background of the ImageView)? I suppose I could use LinearLayout.setOnFocusChangeListener(), but I'm thinking maybe there's a better way, using just XML files.


回答1:


Give something (LinearLayout?) a background image that is a StateListDrawable (drawable XML with <selector> root element). Perhaps use just a transparent color for the normal case, but use something else for the android:focused="true" case. Then, when it gets the focus, the background will change automatically.



来源:https://stackoverflow.com/questions/1572868/android-change-style-when-focused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!