我有一個4x1可調整大小的小部件。當我點擊圖標時,它工作正常
並打開我的應用程序,但如果我點擊背景,它什麼都不做
任何想法如何使我的背景點擊? 這裏是我的代碼:如何捕捉在Android屏幕上的小部件背景的點擊
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<application>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.MyWidget" android:label="@string/widget_to_home" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myWidgetXml"/>
</receiver>
</application>
</manifest>
myWidgetXml.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/myLayout"
android:initialLayout="@layout/myLayout"
android:minHeight="40dp"
android:minWidth="40dp"
android:resizeMode="horizontal"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen">
</appwidget-provider>
myLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin"
android:layout_margin="0dp"
android:gravity="center"
android:background="@android:drawable/alert_dark_frame"
android:orientation="vertical">
<ImageButton
android:id="@+id/WidgetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/widget_pic" />
<TextView
android:id="@+id/WidgetTextView"
android:textColor="@android:color/white"
android:textSize="14sp"
android:layout_marginTop="6dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
MyWidget.java
public class MyWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds)
updateAppWidget(context, appWidgetManager, appWidgetId);
}
private void updateAppWidget(Context context, AppWidgetManager _appWidgetManager, int WidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myLayout);
Intent intent = new Intent(context, MainActivity.class);
Uri.Builder builder = new Uri.Builder();
intent.setData(builder.build());
intent.setAction(Intent.ACTION_VIEW);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.WidgetButton, pendingIntent);
views.setImageViewResource(R.id.WidgetButton, R.drawable.widget_pic);
views.setTextViewText(R.id.WidgetTextView, "text");
_appWidgetManager.updateAppWidget(WidgetId, views);
}
}
嘗試這一個'機器人:'線性佈局'部分中的'myLayout.xml'中的clickable =「true」''如果它不起作用嘗試將'background'拷貝到'src' – Mikhail
@Mikhail,它沒有工作,但是感謝你的努力: - ) –