扩展AppWidgetProvider的Widget中的TextView Marquee是否可能?

时间:2011-10-23 19:52:50

标签: android android-widget widget textview marquee

我对Android编程非常陌生,我到处都读过,我似乎无法找到任何解决方案。

基本问题是我在一个小部件中有一个TextView,我希望文本在文本长于TextView layout_width时滚动。这是我在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);
            }

以下部分用于将内容设置为widget_layout.xml,然后将setSelected的TextView属性设置为true

                setContentView(R.layout.widget_layout);
                findViewById(R.id.fact).setSelected(true);

然后我将ContentView返回给main.xml

现在我猜这是错的,这不是它应该做的方式。但我想知道是否可以做到。我还读到,如果你可以覆盖框架,你可以把你自己的属性,例如ScrollView,这也是正确的吗?我也在SDK Version 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>

3 个答案:

答案 0 :(得分:28)

解决。这就是它的完成方式

在文本视图的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>

答案 1 :(得分:1)

enter image description here

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:lines="1"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="***************Android Dhina Marquee  effect in textview ***************************** "
    android:textColor="#9bebe4" />

如需更多参考,请点击此处http://androiddhina.blogspot.in/2015/10/marquee-effect-in-android-textview.html

答案 2 :(得分:0)

您正在拨打setContentView两次:

setContentView(R.layout.widget_layout);
findViewById(R.id.fact).setSelected(true);
setContentView(R.layout.main);

您的活动只能有一个布局。如果widget_layout是包含文本视图的窗口小部件的布局,那么您不需要第二个setContentView

相关问题