文字跑马灯

MarqueeTextView.java  //新建一个自定义控件,继承TextView就行
public class MarqueeTextView extends android.support.v7.widget.AppCompatTextView{
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

bfrag.xml   //在你要显示的xml调用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f"
    android:gravity="center"
    android:orientation="vertical">

    <fanruiqi.bwie.com.week220181205.util.MarqueeTextView
        android:id="@+id/tv_runHorseLamp1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="这是一段很长的威武霸气的滚动的实现跑马灯效果的一段逼格很高的很有含义和涵养的文字"
        />

    <ImageView
        android:id="@+id/b_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        />

    <Button
        android:id="@+id/b_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="退出登录"/>
</LinearLayout>

 

在Android中,文字跑马灯效果通常指的是文本在某个视图(如TextView)中循环滚动的动画效果。这种效果可以通过几种不同的方法实现,一种简单的方式是使用Android的XML属性来实现静态的跑马灯效果,而更复杂的效果则可能需要使用动画或者定时器等编程手段。 具体操作方法如下: 1. 使用XML属性:在布局文件中的TextView标签中,可以通过设置`android:ellipsize="marquee"`属性来启用跑马灯效果,同时设置`android:singleLine="true"`确保文本在单行显示,并通过`android:marqueeRepeatLimit`属性来控制跑马灯重复的次数(如果设置为`marquee_forever`,则会无限重复)。此外,通过设置`android:scrollHorizontally="true"`属性使得文本水平滚动。 2. 动态编程控制:如果需要更复杂的控制,例如在用户交互时开始或停止跑马灯,可以在Activity或Fragment中使用`Handler`或`Timer`等来定时更新文本的显示位置,从而实现跑马灯效果。同时,还需要监听TextView的`setOnFocusChangeListener`或者`setOnClickListener`来控制跑马灯的开始和停止。 示例代码: ```xml <TextView android:id="@+id/marquee_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里是一段跑马灯文本,它会自动循环滚动显示" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:focusable="true" android:focusableInTouchMode="true"/> ``` 需要注意的是,从Android API 16开始,`android:ellipsize="marquee"`属性已被弃用,建议使用`TextView`的`setEllipsize`方法配合`setSingleLine`方法来实现跑马灯效果,并且要注意处理API版本兼容问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值