2012-04-09 36 views
0

在我的手機上安裝(也發生在仿真器上)後,Widget只有1x1個單元。爲Android創建簡單的小部件。爲什麼widget總是1x1單元大?

我已經嘗試完全卸載應用程序,然後再次安裝它,但創建它的實例時,該小部件仍然只有1x1大小。

任何想法?

這裏是我的供應商信息的XML文件(callernator_widget_provider_info.xml):

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"> 
    android:minWidth="294dp" 
    android:minHeight="294dp" 
    android:updatePeriodMillis="86400000" 
    android:initialLayout="@layout/widgetmain" 
</appwidget-provider> 

這裏是佈局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="32dp" 
     android:text="Bertus van Zyl" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="32dp" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="32dp" 
     android:text="Button" /> 

</LinearLayout> 

這裏是我的清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="android.callernator" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-permission 
     android:name="android.permission.READ_CONTACTS"> 
    </uses-permission> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <receiver android:name=".CallernatorWidgetProvider" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 
      <meta-data android:name="android.appwidget.provider" android:resource="@xml/callernator_widget_provider_info" />  
     </receiver> 
     <activity android:name=".CallernatorActivity" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

回答

0

首先你創建你的小部件佈局xml文件..

例如::我創建widget_layout.xml

`

<RelativeLayout 
    android:id="@+id/relativeLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="1dp" 
    android:layout_marginRight="1dp" 
    android:layout_marginTop="1dp" 
    > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:gravity="center_horizontal" 
     android:text="Title" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="@color/white" 
     android:typeface="serif" /> 
</RelativeLayout> 

<LinearLayout 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginBottom="1dp" 
    android:layout_marginLeft="1dp" 
    android:layout_marginRight="1dp" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/update" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/relativeLayout2" 
     android:gravity="center" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textColor="@color/black" 
     android:typeface="serif" /> 
</LinearLayout> 

`

加入callernator_widget_provider_info.xml後...

機器人:initialLayout = 「@佈局/ widget_layout」

+0

謝謝你的迴應。我已經實施了這些更改,並更新了操作步驟以反映它們。該小部件的尺寸仍然只有1x1。我卸載了安裝的版本,重新啓動了手機,並再次安裝以確保沒有舊的設置或其他東西在浮動。 – 2012-04-09 14:40:23

5

您在窗口小部件提供程序的錯誤位置有右括號>。它應該在聲明的最後,而不是在第一行的末尾。

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="294dp" 
    android:minHeight="294dp" 
    android:updatePeriodMillis="86400000" 
    android:initialLayout="@layout/widgetmain"> 
</appwidget-provider> 

我有完全相同的問題 - 小部件總是出現在1個單元格中。在我的情況下,原因是在小部件提供者的URL中缺少http://。

記住:在您的設備/仿真器上重新添加小部件後,小部件大小更改將生效。

0
  1. 在Android 4.0及以上的原生系統,如果你的manifest.xml有action android:name="android.intent.action.CREATE_SHORTCUT",然後1x1的細胞就會出現。

  2. action android:name="android.intent.action.CREATE_SHORTCUT"是當您長按主屏幕時添加快捷方式。

這兩個功能相互衝突。

相關問題