Android Widget:在4.0中工作但在2.2中不工作

时间:2013-06-27 06:21:49

标签: android android-widget android-appwidget

我已经在Android 4.0中创建并成功运行了自己的Android Widget。 为了测试我在Android 2.2中运行它,但它没有显示我的小部件。它显示Problem Loading Widget消息。 我已将最小和目标sdk设置为

<uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

我的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widgetLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/new_widget_bg"
    android:gravity="right" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >
         <LinearLayout
            android:id="@+id/title_holder"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
             <LinearLayout
                android:id="@+id/image_holder"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="8" >

                <ImageView
                    android:id="@+id/widget_refresh_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/addwidbutton" />

                <ProgressBar
                    android:id="@+id/widget_loading"
                    style="?android:attr/progressBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="32dp"
                    android:visibility="gone" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/main_title_holder"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

 <TextView
     android:id="@+id/title"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center_vertical"
     android:layout_weight="1"
     android:maxLines="1"
     android:text="Default Project"
     android:textAppearance="?android:attr/textAppearanceSmall"
     android:textColor="#ff00a2ff"
     android:textStyle="bold" />

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1.75"
                    android:paddingBottom="5dp"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:paddingTop="5dp" >

                    <Button
                        android:id="@+id/widget_hrs"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_gravity="center"
                        android:layout_weight="2.5"
                        android:background="@drawable/hrs_bg"
                        android:drawableLeft="@drawable/clock"
                        android:maxLines="1"
                        android:text=" --:--"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="#ffffff"
                        android:textStyle="bold" />

                </RelativeLayout>
            </LinearLayout>
        </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="1sp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout1"
        android:background="#66ffffff" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout2" >

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/widget_project"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:text="project_name"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#ffffff"
                android:textStyle="bold" />



            <TextView
                android:id="@+id/widget_error"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:text="Error Message comes here"
                android:textColor="#ff0000"
                android:textStyle="bold"
                android:visibility="gone" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2.25"
            android:orientation="vertical" >

            <Button
                android:id="@+id/widget_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/blue_button"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:maxLines="1"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="start"
                android:textColor="#ffffff" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
</RelativeLayout>

Widget - onReceive方法

AppWidgetManager manager = AppWidgetManager.getInstance(context);
ComponentName thisWidget = new ComponentName(context, MainWidget.class);
RemoteView remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.main_widget);
remoteViews.setViewVisibility(R.id.widget_refresh_button, View.GONE);
remoteViews.setViewVisibility(R.id.widget_loading, View.VISIBLE);
manager.updateAppWidget(thisWidget, remoteViews);

调用此方法会导致我的窗口小部件无效...

我没有注意到Logcat中有关此问题的任何消息

请告诉我最好的方法......

1 个答案:

答案 0 :(得分:1)

经过漫长的追踪和错误,我发现了错误。 我使用过remoteViews.setViewVisibility(R.id.widget_loading, View.VISIBLE); 此行导致我的小部件无法显示。我已从代码中删除了所有setViewVisibility行。现在它显示.....