[android]父子控件之间响应焦点状态

来源:互联网 发布:java pdf转图片 编辑:程序博客网 时间:2024/06/05 10:06

在工作中遇到问题,记录一下解决方案,以备以后使用

问题1

问题类别:UI显示

问题描述:父控件layout的边框颜色 随 子控件editText的 焦点状态 进行切换

解决方法:父控件layout使用addStatesFromChildren属性

代码:

<LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="10dp"    android:background="@drawable/select_background"    android:orientation="horizontal"    android:clickable="true"    android:focusable="true"    android:focusableInTouchMode="true"    android:addStatesFromChildren="true">    <EditText        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Name" /></LinearLayout>

问题2

问题类别:UI显示

问题描述:子控件的背景 随 父控件layout 焦点状态 进行切换

解决方法:父控件layout使用clickable,focusableInTouchMode等属性;子控件使用duplicateParentState属性

代码:
<LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="10dp"    android:background="@drawable/select_background"    android:orientation="horizontal"    android:clickable="true"    android:focusable="true"    android:focusableInTouchMode="true">    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/selector_circle_1"        android:text="TextView"        android:duplicateParentState="true" /></LinearLayout>

注意:duplicateParentState和addStatesFromChildren属性不能在同一对父子控件中使用,会异常

原创粉丝点击