Background Android如何以编程方式设置小部件的圆角

Background Android如何以编程方式设置小部件的圆角,background,widget,Background,Widget,我的widget类是这样的 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main); Intent intent = new Intent(context, widget.

我的widget类是这样的

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
    Intent intent = new Intent(context, widget.class);
    intent.setAction(ACTION_SIMPLEAPPWIDGET);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.widget, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);

}

@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (final int appWidgetId : appWidgetIds) {

        updateAppWidget(context, appWidgetManager, appWidgetId);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
        Intent intent = new Intent(context, widget.class);
        intent.setAction(ACTION_SIMPLEAPPWIDGET);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.widget, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);

    }

}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
    Intent intent1 = new Intent(context, widget.class);
    intent1.setAction(ACTION_SIMPLEAPPWIDGET);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    
    views.setOnClickPendingIntent(R.id.widget, pendingIntent1);

    ComponentName appWidget = new ComponentName(context, widget.class);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(appWidget, views);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="3dp"
    android:layout_gravity="center"
    android:src="@drawable/beyin_ap" />

<TextView
    android:id="@+id/widget_words"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="0.89"
    android:contentDescription="Infinity"
    android:gravity="center"
    android:text="@string/app_name"
    android:textColor="#ffffff"
    android:textSize="20sp" />

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="30dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    

    <Button
        android:id="@+id/widget"
        android:layout_width="30dp"
        android:layout_height="25dp"
        android:layout_marginEnd="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:layout_gravity="top"
        android:background="@drawable/kirmizi"
        android:text="≡"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    
</LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
<corners android:radius="15dp"/>

<gradient
    android:angle="45"
    android:centerColor="#27cbff"
    android:centerX="35%"
    android:endColor="#00a8b1"
    android:startColor="#84aaff"
    android:type="linear"/>
像这样的Wiget-XML

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
    Intent intent = new Intent(context, widget.class);
    intent.setAction(ACTION_SIMPLEAPPWIDGET);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.widget, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);

}

@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (final int appWidgetId : appWidgetIds) {

        updateAppWidget(context, appWidgetManager, appWidgetId);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
        Intent intent = new Intent(context, widget.class);
        intent.setAction(ACTION_SIMPLEAPPWIDGET);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.widget, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);

    }

}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_main);
    Intent intent1 = new Intent(context, widget.class);
    intent1.setAction(ACTION_SIMPLEAPPWIDGET);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    
    views.setOnClickPendingIntent(R.id.widget, pendingIntent1);

    ComponentName appWidget = new ComponentName(context, widget.class);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(appWidget, views);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="3dp"
    android:layout_gravity="center"
    android:src="@drawable/beyin_ap" />

<TextView
    android:id="@+id/widget_words"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="0.89"
    android:contentDescription="Infinity"
    android:gravity="center"
    android:text="@string/app_name"
    android:textColor="#ffffff"
    android:textSize="20sp" />

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="30dp"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    

    <Button
        android:id="@+id/widget"
        android:layout_width="30dp"
        android:layout_height="25dp"
        android:layout_marginEnd="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:layout_gravity="top"
        android:background="@drawable/kirmizi"
        android:text="≡"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    
</LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
<corners android:radius="15dp"/>

<gradient
    android:angle="45"
    android:centerColor="#27cbff"
    android:centerX="35%"
    android:endColor="#00a8b1"
    android:startColor="#84aaff"
    android:type="linear"/>