【问题标题】:Textview marquee programmatically以编程方式 Textview 选取框
【发布时间】:2017-06-15 01:24:09
【问题描述】:

尝试从数组中填充文本视图。我设法通过下面的代码通过 XML 获得所需的效果

<?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>

但我需要其中的几个在一个数组中 - 所以我相信以编程方式创建文本视图可能是答案,但不能让它们选框。这是我正在处理的代码。

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);
        }

【问题讨论】:

    标签: android android-layout listview textview marquee


    【解决方案1】:

    试试这个..它有效

    TextView testing = (TextView) this.findViewById(R.id.testing);
    testing.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    testing.setMarqueeRepeatLimit(-1);
    testing.setSingleLine(true);
    testing.setSelected(true);
    

    【讨论】:

    • 注:setMarqueeRepeatLimit(-1);与 XML 中的 android:marqueeRepeatLimit="marquee_forever" 相同
    【解决方案2】:

    试试这个。

    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);
    

    【讨论】:

    • 感谢您的及时回复,但似乎不起作用 - 没有滚动。
    【解决方案3】:

    使用此代码。我改进了你的代码,享受复制粘贴。

        val strings = arrayOf("Mark", "James", "Paul")
        val layout = findViewById<View>(R.id.linear) as LinearLayout
        layout.orientation = LinearLayout.VERTICAL
        for (s in strings) {
            val newTextView = TextView(this)
    
            newTextView.layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
    
            newTextView.text = s
            newTextView.isSingleLine = true
            newTextView.ellipsize = TextUtils.TruncateAt.END
            newTextView.setHorizontallyScrolling(true)
            newTextView.setLines(1)
            newTextView.marqueeRepeatLimit = -1
            newTextView.isSelected = true
            newTextView.textSize = 24f
            
            addMarquee(newTextView)
            
            layout.addView(newTextView)
        }
    

    将此函数添加到您的类中

        fun addMarquee(textView: TextView) {
        textView.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                val pixels = textView.measuredWidth - 1
                val params = textView.layoutParams
                params.width = pixels
                textView.layoutParams = params
                textView.isSelected = true
                textView.ellipsize = TextUtils.TruncateAt.MARQUEE
                textView.isSingleLine = true
                textView.marqueeRepeatLimit = -1
                textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2017-09-21
      • 2011-08-11
      • 2013-05-18
      • 2011-03-09
      相关资源
      最近更新 更多