addStatesFromChildren 和跑马灯
16lz
2021-12-04
阅读更多
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:gravity ="center_vertical"
android:background ="@drawable/f_background"
android:layout_width ="fill_parent"
android:focusable ="true"
android:layout_height ="50px" >
< TextView
android:id ="@+id/info_text"
android:focusable ="true"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="test marquee .. "
android:textColor ="@color/black"
android:singleLine ="true"
android:ellipsize ="marquee"
android:marqueeRepeatLimit ="3"
android:textSize ="18sp"
/>
< TextView
android:id ="@+id/date_text"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="bottom"
android:textColor ="@color/gray"
android:text ="2010/05/28"
android:textSize ="12sp"
/>
LinearLayout >
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:gravity ="center_vertical"
android:background ="@drawable/zixun_background"
android:layout_width ="fill_parent"
android:addStatesFromChildren ="true"
android:layout_height ="50px" >
< TextView
android:id ="@+id/info_text"
android:focusable ="true"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text =" "
android:textColor ="@color/black"
android:singleLine ="true"
android:ellipsize ="marquee"
android:marqueeRepeatLimit ="3"
android:textSize ="18sp"
/>
< TextView
android:id ="@+id/date_text"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="bottom"
android:textColor ="@color/gray"
android:text ="2010/05/28"
android:textSize ="12sp"
/>
LinearLayout >
在Android中要显示跑马灯是比较容易的,只要设置2个属性就可以了:
android:singleLine="true"
android:ellipsize="marquee"
但 是要显示跑马灯该View必需是可以取得焦点的,只有在取得焦点的情况下跑马灯才会出现.
如果是组合View的情况下就有问题了, 如下一个组合View:
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:gravity ="center_vertical"
android:background ="@drawable/f_background"
android:layout_width ="fill_parent"
android:focusable ="true"
android:layout_height ="50px" >
< TextView
android:id ="@+id/info_text"
android:focusable ="true"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="test marquee .. "
android:textColor ="@color/black"
android:singleLine ="true"
android:ellipsize ="marquee"
android:marqueeRepeatLimit ="3"
android:textSize ="18sp"
/>
< TextView
android:id ="@+id/date_text"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="bottom"
android:textColor ="@color/gray"
android:text ="2010/05/28"
android:textSize ="12sp"
/>
LinearLayout >
上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的 android:focusable=
"true"
也是 没用的. 这个时候就要使用addStatesFromChildren 这个属性了,在LinearLayout中设置这个属性,然后设置TextView的focusable=
"true"
就可以了.关于 addStatesFromChildren的说明:
Sets whether
this
ViewGroup's drawable states
also include its children's drawable states.
可以正常显示的代码:
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:gravity ="center_vertical"
android:background ="@drawable/zixun_background"
android:layout_width ="fill_parent"
android:addStatesFromChildren ="true"
android:layout_height ="50px" >
< TextView
android:id ="@+id/info_text"
android:focusable ="true"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text =" "
android:textColor ="@color/black"
android:singleLine ="true"
android:ellipsize ="marquee"
android:marqueeRepeatLimit ="3"
android:textSize ="18sp"
/>
< TextView
android:id ="@+id/date_text"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_gravity ="bottom"
android:textColor ="@color/gray"
android:text ="2010/05/28"
android:textSize ="12sp"
/>
LinearLayout >
更多相关文章
- Android(安卓)中自定义控件和属性(attr.xml,declare-styleable,T
- android 设置背景图片
- Android(Java):EditText相关
- Android的layout_weight属性释疑
- 那些年,在Fragment中犯的错
- Android(安卓)TextView属性详解
- Android(安卓)中 PopupWindow的用法 汇总 Android(安卓)中 Popup
- Android(安卓)SDK Installed On Mac
- android EditText被键盘遮住的问题