Marquee

今天做一个跑马灯效果(marquee)的TextView,布局文件.xml中代码如下:

 


    

activity的java文件中代码如下:

package com.yiheng.marqueedemo;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {    
    private TextView textView;    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text1);
        textView.setSelected(true);
    }
}

在写的时候发现一个问题就是,在Android Studio中将文本内容单行显示推荐的写法是:

android:maxLines = "1";

但是写完后发现跑马灯效果并没有实现,在网上找了很多解决方法都不管用,最后将代码换成:

android:singleLine = "true"

终于实现跑马灯效果.
但此时AS会提示:

android:singleLine is deprecated: Use maxLines="1" instead
Deprecated views, attributes and so on are deprecated because there is a better way to do something. Do it that new way. You've been warned.

查看singleLine和maxLines的文档:

android:singleLine

Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key. The default value is false (multi-line wrapped text mode) for non-editable text, but if you specify any value for inputType, the default is true (single-line input field mode).
Must be a boolean value, either "true" or "false".
This may also be a reference to a resource (in the form"@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol singleLine
.
Related methods:
setTransformationMethod(TransformationMethod)


android:maxLines

Makes the TextView be at most this many lines tall. When used on an editable text, the inputType attribute's value must be combined with thetextMultiLine
flag for the maxLines attribute to apply.
Must be an integer value, such as "100".
This may also be a reference to a resource (in the form"@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol maxLines
.
Related methods:
setMaxLines(int)

你可能感兴趣的:(Marquee)