Android中的字幕文本

BIB*_*RAL 58 android

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

dro*_*ren 123

这是一个例子:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

带有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>
Run Code Online (Sandbox Code Playgroud)

  • marquee在模拟器上无法使用此代码 (4认同)
  • 此代码在 android 模拟器中运行良好。 (2认同)

Ash*_*and 24

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);
Run Code Online (Sandbox Code Playgroud)


Mau*_*ijk 22

android:ellipsize="marquee"
Run Code Online (Sandbox Code Playgroud)

这只适用于您TextView有焦点的情况.


小智 10

为了实现这一点,我必须使用已经提到的所有三个东西(ellipsize,selected和singleLine):

TextView tv = (TextView)findViewById(R.id.someTextView);
tv.setSelected(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSingleLine(true):
Run Code Online (Sandbox Code Playgroud)


Chr*_*ock 9

只需将这些参数放到TextView中即可 - 它有效:D

android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

而且你还需要setSelected(true):

my_TextView.setSelected(true);
Run Code Online (Sandbox Code Playgroud)

问候,克里斯托弗


Prv*_*rvN 8

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>
Run Code Online (Sandbox Code Playgroud)

Java的

txtEventName.setSelected(true);
Run Code Online (Sandbox Code Playgroud)

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

txtEventName.setText("\t \t \t \t \t \t"+eventName+"\t \t \t \t \t \t");
Run Code Online (Sandbox Code Playgroud)


Aks*_*yay 7

我发现marquee的问题是它不能用于短字符串(因为它的功能只是Show Long字符串).

我建议使用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
`
Run Code Online (Sandbox Code Playgroud)

main_activity.xml代码:

<WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/web"
        ></WebView>
Run Code Online (Sandbox Code Playgroud)


Aks*_*pra 5

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

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
Run Code Online (Sandbox Code Playgroud)

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

setSelected(true);
Run Code Online (Sandbox Code Playgroud)

这就是你所需要的。