转自:http://blog.csdn.net/qq435757399/article/details/8811098
今天用到AndroidTextView的跑马灯效果,在原项目的Layout布局中加了一个跑马灯文本,奇了怪了,文字能出现就是不给我跑起来,又重建了个项目测试,它又能跑了!活见鬼了!!!!
1.下面是测试项中的布局文件:
- <span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/indexgallerytv"
- android:layout_width="200dip"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:ellipsize="marquee"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:gravity="left|center"
- android:marqueeRepeatLimit="marquee_forever"
- android:paddingLeft="20dip"
- android:paddingRight="20dip"
- android:textColor="#000000"
- android:scrollHorizontally="true"
- android:text="多少次迎着冷眼与嘲笑,从没有放弃过心中的理想,一刹那恍惚,若有所失的感觉,不知不觉已变淡,心里爱"
- android:textSize="20dip" />
- </RelativeLayout></span>
2.下面是加到的项目的布局文件中:(此处为错误的布局;下面贴有正确的布局)
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffffff" >
- <include
- android:id="@+id/title"
- android:layout_alignParentTop="true"
- layout="@layout/main_title" />
- <LinearLayout
- android:id="@+id/top_layout"
- android:layout_width="fill_parent"
- android:layout_height="120dip"
- android:layout_below="@+id/title"
- android:layout_margin="5dip"
- android:background="@drawable/integral"
- android:text="@string/hello_world"
- tools:context=".MainActivity" />
- <GridView
- android:id="@+id/grid_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/top_layout"
- android:layout_marginLeft="1dip"
- android:layout_marginRight="1dip"
- android:cacheColorHint="#00000000"
- android:columnWidth="5dip"
- android:numColumns="3" >
- </GridView>
- <TextView
- android:id="@+id/indexgallerytv"
- android:layout_width="200dip"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:ellipsize="marquee"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:gravity="left|center"
- android:marqueeRepeatLimit="marquee_forever"
- android:paddingLeft="20dip"
- android:paddingRight="20dip"
- android:textColor="#000000"
- android:scrollHorizontally="true"
- android:text="多少次迎着冷眼与嘲笑,从没有放弃过心中的理想,一刹那恍惚,若有所失的感觉,不知不觉已变淡,心里爱"
- android:textSize="20dip" />
- </RelativeLayout>
3.后来网上查了下 巴士上面有人给了一个Demo,以上的问题主要是TextView不能获取焦点,不能跑起来,现在重写一下TextView中的方法,让它不获取焦点一样也可以跑起来
- package com.jun.widget;
- import android.content.Context;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class MarqueeText extends TextView {
- public MarqueeText(Context con) {
- super(con);
- }
- public MarqueeText(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- public boolean isFocused() {
- return true;
- }
- @Override
- protected void onFocusChanged(boolean focused, int direction,
- Rect previouslyFocusedRect) {
- }
- }
4.我项目中的布局:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#ffffff" >
- <include
- android:id="@+id/title"
- android:layout_alignParentTop="true"
- layout="@layout/main_title" />
- <LinearLayout
- android:id="@+id/top_layout"
- android:layout_width="fill_parent"
- android:layout_height="120dip"
- android:layout_below="@+id/title"
- android:layout_margin="5dip"
- android:background="@drawable/integral"
- android:text="@string/hello_world"
- tools:context=".MainActivity" >
- </LinearLayout>
- <GridView
- android:id="@+id/grid_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@+id/pao"
- android:layout_below="@+id/top_layout"
- android:layout_marginLeft="1dip"
- android:layout_marginRight="1dip"
- android:cacheColorHint="#00000000"
- android:columnWidth="5dip"
- android:numColumns="3" >
- </GridView>
- <com.jun.widget.MarqueeText
- android:id="@+id/pao"
- android:layout_width="200dip"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:ellipsize="marquee"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:gravity="left|center"
- android:marqueeRepeatLimit="marquee_forever"
- android:paddingLeft="20dip"
- android:paddingRight="20dip"
- android:scrollHorizontally="true"
- android:text="多少次迎着冷眼与嘲笑,从没有放弃过心中的理想,一刹那恍惚,若有所失的感觉,不知不觉已变淡,心里爱"
- android:textColor="#000000"
- android:textSize="20dip" />
- </RelativeLayout>
经过以上修改 跑马灯就跑起来了!哈哈