简体   繁体   中英

My App Widget doesn't Placed on the left side of Home Screen

I have created an app widget for my app. All works fine, the only problem is that when I show my app widget on the home screen and try to place the app widget in the left side of the home screen it doesn't place, but I am able to place it at top center, Middle center, bottom center. So why I am not able to move it to the left side. Can anyone help me to solve this out? I am sending my code along with the screenshots.

Code For Widget Layout

<?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 Code

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;
    }
}
}

Screen Shots在此处输入图片说明

Try reducing the android:minWidth to 56dp.

You are telling the widget to take up a width of at least 272dp on the screen.

If you only need a 1x1 cell, it is really enough to make android:minHeight="40dp" and android:minWidth="40dp"

Look here for more information: http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#anatomy_determining_size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM