Textview以编程方式选框 [英] Textview marquee programmatically

查看:115
本文介绍了Textview以编程方式选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从数组中填充文本视图.我设法通过下面的代码通过XML达到了预期的效果

Trying to populate an textview(s) from an array. I managed to get the desired effect via XML via the code below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/marque_scrolling_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="16dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Create a Marquee (Scrolling Text) in Android Using TextView. Android Marquee (Scrolling Text) Tutorial with Example"
        android:textSize="24sp" />
</LinearLayout>

但是我需要一个数组中的几个-因此我相信以编程方式创建textviews可能是答案,但无法使它们脱颖而出.这是我正在处理的代码.

But I need several of these in an array - so I believe creating the textviews programmatically may be the answer, but cant get them to marquee. This is the code im working on.

String[] strings = {"Mark", "James", "Paul"};
        LinearLayout layout = (LinearLayout)findViewById(R.id.linearlayout);


        for(String s : strings)
        {
             TextView newTextView = new TextView(this);
             newTextView.setText(s);
             newTextView.setSingleLine(true);
             newTextView.setEllipsize(TextUtils.TruncateAt.END);       
             newTextView.setHorizontallyScrolling(true);
             newTextView.setLines(1);
             newTextView.setMarqueeRepeatLimit(-1);
             newTextView.setSelected(true);  
             newTextView.setTextSize(24);
             layout.addView(newTextView);
        }

推荐答案

尝试一下.

final RelativeLayout relativeLayout = new RelativeLayout(this);
final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
textView = new TextView(this);
textView.setId(1);

RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeLayoutbotombar.setLayoutParams(relativlaybottombar);


        textView.setText("text text text text text, with a long ");
        textView.setEllipsize(TruncateAt.MARQUEE);
        textView.setSelected(true);
        textView.setSingleLine(true);


        relativeLayout.addView(relativeLayoutbotombar);
        relativeLayoutbotombar.addView(textView);
        setContentView(relativeLayout, relativlayparamter);

另一个选择:

TextView textView = (TextView) this.findViewById(R.id.xxx);  
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("Text text text text");
textView.setSelected(true);
textView.setSingleLine(true);

这篇关于Textview以编程方式选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆