Android TextView跑马灯+悬浮窗口

首先自定义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) {  
		}


}


在XML布局中TextView

  android:id="@+id/TextView"

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"

            android:textColor="@android:color/white"
        android:textSize="22dp"        
        android:ellipsize="marquee" 
        android:focusable="true" 
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever" 
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"

代码

		mqt=(MarqueeText) floatView.findViewById(R.id.TextView);
		 mqt.setMovementMethod(ScrollingMovementMethod.getInstance()); // Android开发网提示相关的可以查看
		mqt .setMarqueeRepeatLimit(6);

将TextView设置成悬浮窗口

private void createFloatView() {
		mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
		sW = mWindowManager.getDefaultDisplay().getWidth();
		sH = mWindowManager.getDefaultDisplay().getHeight();
		initWindowParams();
		LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
		floatView = inflater.inflate(R.layout.bo_view, null);
		mqt=(MarqueeText) floatView.findViewById(R.id.TextView);
		 mqt.setMovementMethod(ScrollingMovementMethod.getInstance()); // Android开发网提示相关的可以查看
		mqt .setMarqueeRepeatLimit(6);
		mWindowManager.addView(floatView, wmParams);
		
	}

	private void initWindowParams() {
		wmParams = new LayoutParams();
		wmParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
		wmParams.format = PixelFormat.RGBA_8888;
		wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
		wmParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
		wmParams.x = 0;
		wmParams.y = 0;
		wmParams.width = WindowManager.LayoutParams.FILL_PARENT;
		wmParams.height =  60;
		eatParams = new LayoutParams();
		eatParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
		eatParams.format = PixelFormat.RGBA_8888;
		eatParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
		eatParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
		eatParams.x = 0;
		eatParams.y = sH / 2 - 150;
		eatParams.width = WindowManager.LayoutParams.FILL_PARENT;
		eatParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  ;
	}


你可能感兴趣的:(Android TextView跑马灯+悬浮窗口)