Android TextView 选框不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332924/
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
TextView Marquee not working
提问by amithgc
I have tried to use marquee and its not working here is my code, please let me know where im going wrong
我曾尝试使用选框,但我的代码在这里不起作用,请让我知道我哪里出错了
<TextView
android:text="lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00"
android:id="@+id/TextView02"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:singleLine="true"
android:focusable="true"
android:inputType="text"
android:maxLines="1">
</TextView>
i am using android SDK 2.0.1
我正在使用 android SDK 2.0.1
回答by amithgc
working now :) Code attached below
现在工作:) 下面附上代码
<TextView
android:text="START | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | END"
android:id="@+id/MarqueeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true">
Edit (on behalf of Adil Hussain):
编辑(代表 Adil Hussain):
textView.setSelected(true)
needs to be set in code behind for this to work.
textView.setSelected(true)
需要在后面的代码中设置才能使其工作。
回答by Shardul
android:singleLine="true"
android:ellipsize="marquee"
are the only required attributes and scrolling even works with layout_weight
defined with layout_width=0dp
是唯一必需的属性,滚动甚至可以与layout_weight
定义的一起使用layout_width=0dp
here is some sample code:
这是一些示例代码:
<TextView
android:id="@+id/scroller"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
android:layout_marginLeft="4dp"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
But what is most important is implicitely or explicitely TextView
should get selected.
但最重要的是隐式或显式TextView
应该被选中。
You can do this with:
你可以这样做:
TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);
回答by sajamaum
These attributes must be included in the textview
tag in order to allow scrolling.
这些属性必须包含在textview
标签中才能允许滚动。
Everything else is optional.
其他一切都是可选的。
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:ellipsize="marquee"
回答by Dulal
I faced the same problem and this discussion helped me I just replace this line
我遇到了同样的问题,这个讨论帮助我我只是替换了这条线
android:maxLines="1"
with this line in xml
在 xml 中使用这一行
android:singleLine="true"
and it works
the line txtView.setSelected(true);
was also in my activity.
它的工作原理txtView.setSelected(true);
也在我的活动中。
回答by Shihab Uddin
Very Simple working code:
非常简单的工作代码:
For infinitely scrolling text
用于无限滚动文本
<TextView
android:id="@+id/textView_News_HeadLine"
style="@style/black_extra_large_heading_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="-1"
android:singleLine="true"
android:text="HeadLine: Banglawash To be Continued" />
& you should must write from your activity
&你必须从你的活动中写作
textView.setSelected(true);
回答by susemi99
<TextView
android:ellipsize="marquee"
android:singleLine="true"
.../>
must call in code
必须调用代码
textView.setSelected(true);
回答by Vijay E
I had gone through this situation where textview marquee was not working. However follow this and I am sure it will work. :)
我经历过这种 textview 选框不起作用的情况。但是,请遵循此操作,我相信它会起作用。:)
<TextView
android:id="@+id/tv_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="This is a sample code of marquee and it works"/>
and programmatically add these 2 lines...
并以编程方式添加这两行...
tvMarquee.setHorizontallyScrolling(true);
tvMarquee.setSelected(true);
tvMarquee.setSelected(true) is required incase if any one of the view is already focused and setSelected will make it work. No need to use.
tvMarquee.setSelected(true) 是必需的,如果任何一个视图已经聚焦并且 setSelected 将使其工作。没有必要使用。
android:singleLine="true"
it is deprecated and above codes works.
它已被弃用,上面的代码有效。
回答by Kirit Vaghela
Working Code:
工作代码:
<TextView
android:id="@+id/scroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
android:textAppearance="?android:attr/textAppearanceLarge" />
回答by ssdscott
I'm working with minSDK=14 and was curious what set of these variations would work. I ended up with:
我正在使用 minSDK=14 并且很好奇这些变体的哪些组会起作用。我结束了:
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
in addition to other formatting stuff. I didn't need scrollHoriontally, focusable, or focusableInTouchMode.
除了其他格式化的东西。我不需要 scrollHoriontally、focusable 或 focusableInTouchMode。
This set did require a call to
这个集合确实需要调用
setSelected(true)
What I find interesting is that singleLine has allegedly been deprecated, with a recommendation to replace it with maxLines = 1. Except - when I do that, that change alone stops the text from scrolling. One would hope that when singleLine eventually bites the dust, that all its current behavior will be triggered by maxLines...
我觉得有趣的是,据称 singleLine 已被弃用,建议将其替换为 maxLines = 1。除了 - 当我这样做时,仅此更改即可阻止文本滚动。人们希望当 singleLine 最终尘埃落定时,它所有当前的行为都将由 maxLines 触发......
回答by David Roman
I've encountered the same problem. Amith GC's answer(the first answer checked as accepted) is right, but sometimes textview.setSelected(true); doesn't work when the text view can't get the focus all the time. So, to ensure TextView Marquee working, I had to use a custom TextView.
我遇到了同样的问题。Amith GC 的答案(第一个被检查为接受的答案)是正确的,但有时 textview.setSelected(true); 当文本视图不能一直获得焦点时不起作用。因此,为了确保 TextView Marquee 正常工作,我必须使用自定义 TextView。
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
}
And then, you can use the custom TextView as the scrolling text view in your layout .xml file like this:
然后,您可以使用自定义 TextView 作为布局 .xml 文件中的滚动文本视图,如下所示:
<com.example.myapplication.CustomTextView
android:id="@+id/tvScrollingMessage"
android:text="@string/scrolling_message_main_wish_list"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/black"
android:gravity="center"
android:textColor="@color/white"
android:textSize="15dp"
android:freezesText="true"/>
NOTE: in the above code snippet com.example.myapplication is an example package name and should be replaced by your own package name.
注意:在上面的代码片段中 com.example.myapplication 是一个示例包名称,应替换为您自己的包名称。
Hope this will help you. Cheers!
希望这会帮助你。干杯!