Android小部件:在4.0中工作,但在2.2中不工作
我已经在安卓4.0中创建并成功运行了自己的安卓小部件。 为了进行测试,我在Android 2.2中运行了它,但它没有显示我的小部件。它显示加载小部件时出现问题的消息。 我已将最小和目标sdk设置为Android小部件:在4.0中工作,但在2.2中不工作,android,android-widget,android-appwidget,Android,Android Widget,Android Appwidget,我已经在安卓4.0中创建并成功运行了自己的安卓小部件。 为了进行测试,我在Android 2.2中运行了它,但它没有显示我的小部件。它显示加载小部件时出现问题的消息。 我已将最小和目标sdk设置为 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> 调用此方法导致我的小部件无法工作 我在Logcat中没有注意到任何关于这个问题的消息 请给我提供最好的方法…经过长时间
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
调用此方法导致我的小部件无法工作
我在Logcat中没有注意到任何关于这个问题的消息
请给我提供最好的方法…经过长时间的跟踪和错误,我发现了错误。 我使用了
RemoteView.setViewVisibility(R.id.widget\u加载,View.VISIBLE)代码>
这一行导致我的小部件不显示。我已从代码中删除所有setViewVisibility
行。现在它显示了….我曾经在小型设备中面对这个问题,小型设备必须有一个单独的布局,如果您提到您使用的设备,它可能会给我们一个提示,或者尝试另一个虚拟和简单的布局,让小部件在2.2中测试它
<?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>
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);