我的应用程序小组件未放置在主屏幕的左侧

时间:2012-03-01 11:32:47

标签: android android-widget

我为自己的应用创建了一个应用小部件。一切正常,唯一的问题是,当我在主屏幕上显示我的应用程序小部件并尝试将应用程序应用程序小部件放置在主屏幕的左侧时,它没有放置,但我可以将它放在顶部中心,中间中心,底部中心。那么为什么我不能把它移到左侧。任何人都可以帮我解决这个问题。我正在发送我的代码和屏幕截图。

小工具布局代码

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

<ImageView
    android:id="@+id/backgroundImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:contentDescription="@string/tracking_status_faq_imageview_text"
    android:scaleType="fitXY" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal|center_vertical" >

    <TextView
        android:id="@+id/txt_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5f5f5f"
        android:textSize="14dp">
    </TextView>
</LinearLayout>

Java代码

public class Widget extends AppWidgetProvider implements Utilities
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
{      
    // Read data from register before  updating the widget
    CycleManager.getSingletonObject().readHistoryDateFromFile(context);

    // To prevent any ANR timeouts, we perform the update in a service
    context.startService(new Intent(context, UpdateService.class));
}

public static class UpdateService extends Service 
{
    @Override
    public void onStart(Intent intent, int startId) 
    {
        // Build the widget update for today
        RemoteViews updateViews = getValueFromCycleManager(this);

        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
    }

    @Override
    public IBinder onBind(Intent arg0){
        // TODO Auto-generated method stub
        return null;
    }

    public RemoteViews getValueFromCycleManager(Context context) 
    {
        // create instance of calendar instance
        Calendar calInstance = Calendar.getInstance();
        calInstance.set(Calendar.HOUR_OF_DAY, DEFAULT_TIME_OF_DATE);  
        calInstance.set(Calendar.MINUTE, DEFAULT_TIME_OF_DATE);     
        calInstance.set(Calendar.SECOND, DEFAULT_TIME_OF_DATE); 
        calInstance.set(Calendar.MILLISECOND, DEFAULT_TIME_OF_DATE);

        SimpleDateFormat dateformat = new SimpleDateFormat("dd");

        RemoteViews remoteViews = null;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        // set current date to the widgets
        remoteViews.setTextViewText(R.id.txt_date, dateformat.format(new Date()));      

        // set status message in the widget on current date
        int enStage = CycleManager.getSingletonObject().getCycleStage(calInstance.getTime());

        switch (enStage)
        {
            case enSTAGE_NONE:  
            remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.but_active);          
            break;

            case enSTAGE_START:
            remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.but_cycle);
            break;

            case enSTAGE_FLOW:
            remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.but_cycle);
            break;

            case enSTAGE_SAFE:
            remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.but_safe);
            break;

            case enSTAGE_UNSAFE:
            remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.but_unsafe);
            break;

            case enSTAGE_FERTILE:
            remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.but_fertile);
            break;

            default:
            break;
        }

        // When user clicks on widget, launch to Application Main page
        Intent defineIntent = new Intent(context, SplashActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* no requestCode */, defineIntent, 0 /* no flags */);
        remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);

        return remoteViews;
    }
}
}

屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:1)

尝试将android:minWidth减少到56dp。

您告诉小部件在屏幕上占用至少272dp的宽度。

如果您只需要1x1单元格,那么android:minHeight="40dp"android:minWidth="40dp"

就足够了

点击此处了解更多信息: http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#anatomy_determining_size