我的小部件是一个电池级别的小部件..我想要的是根据电池电量显示电池的不同图像。这可能来自我的代码吗?
BatteryStatusWidgetActivity
public class BatteryStatusWidgetActivity extends AppWidgetProvider {
/** Called when the activity is first created. */
private String batteryLevel = "init";
private int widgetImageFrame = R.drawable.widget_batt_frame01;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
updateView(context);
}
@Override
public void onReceive(Context context, Intent intent) {
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = 65;
level = (rawlevel * 100) / scale;
batteryLevel = level + "%";
widgetImageFrame = R.drawable.widget_batt_frame01;
updateView(context);
super.onReceive(context, intent);
}
public void updateView(Context context) {
RemoteViews thisViews = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.widget_layout);
thisViews.setTextViewText(R.id.widget_text, batteryLevel);
thisViews.setImageViewResource(R.id.imageView1, widgetImageFrame);
ComponentName thisWidget = new ComponentName(context, BatteryStatusWidgetActivity.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, thisViews);
}
和widget_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/widget_batt_frame01" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dip"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingTop="0dip" >
<TextView
android:id="@+id/widget_text"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"
android:text="@string/battery_level"
android:textSize="12dp"
android:textColor="#000000"/>
</LinearLayout>
</RelativeLayout>
谢谢。
答案 0 :(得分:1)
我通过显示/隐藏不同的视图在过去的项目中实现了这一点。你的updateView()中有类似的东西:
if (level > 90) {
thisViews.setViewVisibility(widget_frame01, View.VISIBLE);
thisViews.setViewVisibility(widget_frame02, View.GONE);
thisViews.setViewVisibility(widget_frame03, View.GONE);
} else if (level > 80) {
thisViews.setViewVisibility(widget_frame01, View.GONE);
thisViews.setViewVisibility(widget_frame02, View.VISIBLE);
thisViews.setViewVisibility(widget_frame03, View.GONE);
} else {
thisViews.setViewVisibility(widget_frame01, View.GONE);
thisViews.setViewVisibility(widget_frame02, View.GONE);
thisViews.setViewVisibility(widget_frame03, View.VISIBLE);
}
然后,在你的xml中,将所有的图像视图设置为android:visibility="GONE"
,这样它们在默认情况下都是隐藏的,否则你会看到一堆图像在你第一次堆叠在一起设置小部件:
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/widget_batt_frame01"
android:visibility="GONE" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/widget_batt_frame02"
android:visibility="GONE" />
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/widget_batt_frame03"
android:visibility="GONE" />
它不是很优雅,但却是我能让它发挥作用的唯一方式。