是否可以在扩展 AppWidgetProvider 的 Widget 中使用 TextView Marquee?
我对Android编程很陌生,我到处都读过,但似乎找不到任何解决方案。
基本问题是我在小部件中有一个 TextView,并且我希望当文本长于 TextView 布局宽度时文本滚动。这是我在layout_widget.xml 中的代码
<TextView android:id="@+id/fact" android:layout_width="200dp"
android:text="Loading... More text to see if it spans or not and want more"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true" />
现在我读到我必须使TextView 成为焦点,我已经做到了。我还读到您需要设置属性 setSelected(true),这就是我正在努力设置的地方。在我的默认 Activity(在 AndroidManifest.xml 中)中,我有以下代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_layout);
findViewById(R.id.fact).setSelected(true);
setContentView(R.layout.main);
}
下面的部分用于将 Content 设置为 widget_layout.xml,然后将 setSelected 的 TextView 属性设置为 true
setContentView(R.layout.widget_layout);
findViewById(R.id.fact).setSelected(true);
我然后将 ContentView 返回到 main.xml
现在我猜这是错误的,这不是应该的方式完毕。但我想知道是否可以做到。我还读到,如果您可以覆盖框架,您可以将自己的属性放入其中,例如ScrollView,这也是对的吗?另外我使用的是 SDK 版本 7。
非常感谢我收到的帮助,谢谢大家!
编辑:通过删除setContentView(R.layout.main);,当通过应用程序绘制启动应用程序时,文本会滚动,但小部件不会滚动。有点让我意识到小部件不能有字幕???有没有人在小部件上工作过字幕?
编辑2:已解决。这就是它的完成方式
在文本视图的 xml 中,您需要有一个标签 这基本上我认为与 getSeleted(true); 相同
所以代码应该如下:
<TextView android:id="@+id/fact" android:layout_width="200dp"
android:text="Loading... More text to see if it spans or not and want more"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:duplicateParentState="true">
<requestFocus android:focusable="true" android:focusableInTouchMode="true"
android:duplicateParentState="true" />
</TextView>
I am very new to Android programming, and I have read everywhere and I can't seem to find any solution.
Basic problem is that I have a TextView in a widget and I would like the text to scroll when the text is longer than the TextView layout_width. This is my code in the layout_widget.xml
<TextView android:id="@+id/fact" android:layout_width="200dp"
android:text="Loading... More text to see if it spans or not and want more"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true" />
Now I read that I have to make the TextView to be on focus, which I have done. I have also read that you need to set the property setSelected(true), and this is where I am struggling to set. In my default Activity (in the AndroidManifest.xml) I have this following code.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_layout);
findViewById(R.id.fact).setSelected(true);
setContentView(R.layout.main);
}
The part below is used to set the Content to the widget_layout.xml and then set the TextView property for setSelected to true
setContentView(R.layout.widget_layout);
findViewById(R.id.fact).setSelected(true);
I then return the ContentView back to main.xml
Now I am guessing this is wrong and this is not the way it should be done. But I am wondering if it can be done. I also read that if you can override the Framework, you can put your own properties in, for example ScrollView, is this right as well? Also I am on SDK Version 7.
I much appreciate the help I receive, thanks all!
Edit: By removing setContentView(R.layout.main); when launching the application via the app draw, the text does scroll, but the widget doesn't. Kind of leads me to that a widget cannot have a marquee??? Has anyone got a marquee working on a widget??
Edit2: Solved. This is how it is done
In the xml for the text view you need to have a tag This basically I think is the same as getSeleted(true);
So the code should be as followed:
<TextView android:id="@+id/fact" android:layout_width="200dp"
android:text="Loading... More text to see if it spans or not and want more"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:duplicateParentState="true">
<requestFocus android:focusable="true" android:focusableInTouchMode="true"
android:duplicateParentState="true" />
</TextView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了。这就是它的完成方式
在文本视图的 xml 中,您需要有一个标签 这基本上我认为与 getSeleted(true); 相同
所以代码应该如下:
Solved. This is how it is done
In the xml for the text view you need to have a tag This basically I think is the same as getSeleted(true);
So the code should be as followed:
您调用
setContentView
两次:您的 Activity 只能有一种布局。如果
widget_layout
是包含文本视图的小部件的布局,那么您不需要第二个setContentView
。You are calling
setContentView
twice:Your activity can only have one layout. If
widget_layout
is the layout for your widget that includes the text view, then you don't want the secondsetContentView
.