简体   繁体   English

Android TextView 选框不起作用

[英]Android TextView Marquee not working

Marquee not working for my TextView Please check below code Marquee 不适用于我的 TextView 请检查下面的代码

XML Code for TextView TextView XML 代码

                          <TextView
                            android:id="@+id/mtextcash"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="30dp"
                            android:ellipsize="marquee"
                            android:fadingEdge="horizontal"
                            android:gravity="center"
                            android:marqueeRepeatLimit="marquee_forever"
                            android:maxLength="5"
                            android:scrollHorizontally="true"
                            android:scrollbars="horizontal"
                            android:singleLine="true"
                            android:text="Simple application that shows how to use marquee, with a long text"
                            android:textColor="@color/white"
                            android:textColorHint="@color/white"
                            android:textSize="25dp" />

In Activity OnCreate在活动 OnCreate

TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash);
inputAvailableCash.setSelected(true);

Thanks in Advance提前致谢

Since the text is very long and and your code will only work if you write the text in single line.由于文本很长,并且您的代码仅在您将文本写成单行时才有效。 Either add要么添加

 android:singleline="true" 

in xml or change your java code to.在 xml 中或将您的 java 代码更改为。

  TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash);
        inputAvailableCash.setSelected(true);
         inputAvailableCash.setSingleLine(true);

This will surely work for you.这肯定对你有用。

add this attribute in the xml file在xml文件中添加这个属性

android:focusable="true"    
android:focusableInTouchMode="true" 

Once try by putting these params to your TextView - It works一旦尝试将这些参数放入您的 TextView - 它有效

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):而且您还需要设置Selected(true):

 my_TextView.setSelected(true);

For marquee effect width should be "match_parent" or static(like 200dp...etc) always.对于选取框效果,宽度应始终为“match_parent”或静态(如 200dp...等)。 And programatically make it setSelected true as you already done then just make it's width as match_parent in xml it will work.并以编程方式使其 setSelected 为真,就像您已经完成的那样,然后只需将其宽度设置为 xml 中的 match_parent 即可。

Edited xml:编辑的xml:

                     <TextView
                        android:id="@+id/mtextcash"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="30dp"
                        android:ellipsize="marquee"
                        android:fadingEdge="horizontal"
                        android:gravity="center"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:maxLength="5"
                        android:scrollHorizontally="true"
                        android:scrollbars="horizontal"
                        android:singleLine="true"
                        android:text="Simple application that shows how to use marquee, with a long text"
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textSize="25dp" />

If by doing match_parent it will effect at your design then you have to manage for it by fixing it's width or with some other way.如果通过执行 match_parent 它会影响您的设计,那么您必须通过固定它的宽度或其他方式来管理它。

As per your xml code you are using android:maxLength="5" means only 5 character will be entered so you can fix it's width by 50dp or any other static size.根据您使用的 xml 代码 android:maxLength="5" 意味着只会输入 5 个字符,因此您可以将其宽度固定为 50dp 或任何其他静态大小。

Hey check this code but marquee working when your text size ie length is longer.its working at my side.:)嘿,检查此代码,但是当您的文本大小(即长度)较长时,选框会工作。它在我身边工作。:)

 <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:textColor="#2086CA"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
        android:textAppearance="?android:attr/textAppearanceSmall" />

Hope this helps.:)希望这可以帮助。:)

<TextView android:id="@+id/mtextcash"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginLeft="30dp"
                   android:ellipsize="marquee"
                   android:gravity="center"
                   android:marqueeRepeatLimit="marquee_forever"
                   android:scrollHorizontally="true"
                   android:singleLine="true"
                   android:text="Simple application that shows how to use marquee, with a long text"
                   android:textColor="@color/white"
                   android:textColorHint="@color/white"
                   android:textSize="25dp" />

The minimum code to run marquee on TextView isTextView上运行选取框的最小代码是

<TextView
    .
    .
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    />

also do not forget to set selected as below也不要忘记设置selected如下

textview.setSelected(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM