使用Android小部件

时间:2017-07-24 14:34:34

标签: java android xml widget android-widget

我正在尝试安装Android小部件,并尝试制作一个简单的4x2小部件,每天显示不同的谚语,为此我跟着this tutorial,一切似乎工作正常没有任何错误或崩溃,但问题是我似乎无法在我的手机上的小部件部分找到小部件来拖放它到目前为止我的代码:

JAVA

public class Widget extends AppWidgetProvider {
Random rand = new Random();
String [] daily_proverbs = new String[]{
    /** list of proverbs goes here */
};

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int count = appWidgetIds.length;

    for (int i = 0; i < count; i++) {
        int widgetId = appWidgetIds[i];
        String prvb = getRandomProverb();

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.activity_widget);
        remoteViews.setTextViewText(R.id.sayings, prvb);

        Intent intent = new Intent(context, Widget.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}
public String getRandomProverb(){
    return this.daily_proverbs[rand.nextInt(164)];
}
}

XML - 小部件提供商:在这里我收到此警告:The following classes could not be found: -include我尝试通过添加丢失的include来修复它,但没有发生任何事情

<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="140dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/activity_widget"
android:resizeMode="none"
android:widgetCategory="home_screen|keyguard">
</appwidget-provider>

WIDGET LAYOUT XML

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="250dp"
android:padding="8dp"
android:background="@color/white">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="title goes here"
    android:clickable="false"
    android:textColor="@color/white"
    android:background="@color/electricBlue"
    android:layout_alignParentBottom="true"
    android:id="@+id/button2" />

<TextView
    android:id="@+id/sayings"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:layout_weight="1"
    android:gravity="center"
    android:text="proverbs goes here"
    android:background="@color/white"
    android:textSize="@dimen/abc_text_size_large_material"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/button2"
    android:layout_below="@+id/actionButton" />

<Button
    android:id="@+id/actionButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="some text here"
    android:clickable="false"
    android:textColor="@color/white"
    android:background="@color/electricBlue"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

2 个答案:

答案 0 :(得分:1)

您是否在AndroidManifest文件中添加了小部件?

<receiver android:name="Widget" >
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/your_appwidget_info" />
</receiver>

答案 1 :(得分:1)

我没有经历所有这些,而是​​简单地创建了一个小部件:

  

1-右键点击布局

     

2-转到&#34; new&#34; - &GT;小工具 - &gt; App Widget

它和它的一切都将被设置