Android 中的选取框文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2182578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:51:47  来源:igfitidea点击:

Marquee text in Android

android

提问by BIBEKRBARAL

How can I use marquee text in an android application?

如何在 android 应用程序中使用选取框文本?

回答by droidgren

Here is an example:

下面是一个例子:

public class TextViewMarquee extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) this.findViewById(R.id.mywidget);  
        tv.setSelected(true);  // Set focus to the textview
    }
}

The xml file with the textview:

带有文本视图的 xml 文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:maxLines="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>

回答by Ashish Anand

TextView textView = (TextView) this.findViewById(R.id.textview_marquee);  
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("General Information... general information... General Information");
textView.setSelected(true);
textView.setSingleLine(true);

回答by Maurits Rijk

android:ellipsize="marquee"

This only works when your TextViewhas the focus.

这仅在您TextView专注时才有效。

回答by Christopher Stock

Just put these params to your TextView - It works :D

只需将这些参数放入您的 TextView - 它有效:D

android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"

And you also need to setSelected(true):

你还需要setSelected(true)

my_TextView.setSelected(true);

Greetings, Christopher

问候,克里斯托弗

回答by BRG

To get this to work, I had to use ALL three of the things (ellipsize, selected, and singleLine) mentioned already:

为了让它工作,我必须使用已经提到的所有三个东西(椭圆、选择和单线):

TextView tv = (TextView)findViewById(R.id.someTextView);
tv.setSelected(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSingleLine(true):

回答by PrvN

Xml code

xml代码

 <TextView
            android:id="@+id/txtTicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="center_horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:scrollHorizontally="true"
            android:shadowColor="#FF0000"
            android:shadowDx="1.5"
            android:shadowDy="1.3"
            android:shadowRadius="1.6"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold" >
        </TextView>

Java

爪哇

txtEventName.setSelected(true);

if text is small then add space before and after text

如果文本很小,则在文本前后添加空格

txtEventName.setText("\t \t \t \t \t \t"+eventName+"\t \t \t \t \t \t");

回答by Akshay Upadhyay

I found a Problem with marquee that it can't be used for short strings(As its function is only to Show Long strings).

我发现选框有一个问题,它不能用于短字符串(因为它的功能只是显示长字符串)。

I suggest Use Webview If you want to move short strings horizontally. Main_Activity.java Code:`

如果您想水平移动短字符串,我建议使用 Webview。Main_Activity.java 代码:`

        WebView webView;

        webView = (WebView)findViewById(R.id.web);


        String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
                + "Hello Droid" + "</marquee></FONT></html>";

        webView.loadData(summary, "text/html", "utf-8"); // Set focus to the textview
`

main_activity.xml code:

main_activity.xml 代码:

<WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/web"
        ></WebView>

回答by Akshay Chopra

In XML file, you need to add following additional attributes in a TextView to get a marquee like feature:

在 XML 文件中,您需要在 TextView 中添加以下附加属性以获得类似字幕的功能:

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"

In MainActivity.java file, you can get the reference of this TextView by using findViewById() and you can set the following property to this TextView to make it appear like a marquee text:

在 MainActivity.java 文件中,您可以使用 findViewById() 获取此 TextView 的引用,您可以为该 TextView 设置以下属性,使其看起来像一个选取框文本:

setSelected(true);

That's all you need.

这就是你所需要的。

回答by Kumar PG

Add Below Code in XML

在 XML 中添加以下代码

    <TextView
    android:text="Shops NearBy textdf fsdgsdgsdg dsgtsgsdgsdgsg"
    android:id="@+id/txtEventName"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"
    android:singleLine="true"/>

In Java add following Code:

在 Java 中添加以下代码:

    TextView txtEventName=(TextView)findViewById(R.id.txtEventName);
    txtEventName.setSelected(true);

回答by Md Sufi Khan

I have tried all of the above, but for me its didn't work. When I add

我已经尝试了上述所有方法,但对我来说它没有用。当我添加

android:clickable="true"

then it's worked perfectly for me. I don't know why. But I am happy to work it.

那么它对我来说非常有效。我不知道为什么。但我很高兴工作。

Here is my full answer.

这是我的完整答案。

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

android:clickable="true"