【问题标题】:My App Widget doesn't Placed on the left side of Home Screen我的 App Widget 未放置在主屏幕的左侧
【发布时间】:2012-03-01 11:32:47
【问题描述】:

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

小部件布局代码

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

屏幕截图

【问题讨论】:

  • 你的 appwidget-provider xml 是什么样的?
  • schemas.android.com/apk/res/android" android:initialLayout="@layout/widget_layout" android: minHeight="56dp" android:minWidth="272dp" android:updatePeriodMillis="2100000" />
  • @EricNordvik 我已经向你发送了 appwidget-provider xml 代码

标签: android android-widget


【解决方案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

【讨论】:

  • 哇...这么简单,谢谢 Eric...实际上我的小部件尺寸很大,这就是为什么我将最小宽度设置为 272dp 而忘记解决这个问题。
  • 不客气 :) 很容易忘记这些问题,因为 provider-xml 不是您经常更改的文件
猜你喜欢
  • 2022-12-05
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多