2014-02-18 39 views
0

我创建了我的第一个小部件,但当我更改按钮图像时出现问题。 低于它是Java文件的代码部分的部件:Android更改部件按钮

@Override 
public void onReceive(Context context, Intent intent) 
{ 
if (mp == null){ 
mp = MediaPlayer.create(context.getApplicationContext(), R.raw.segno); 
} 

final String action = intent.getAction(); 

if (ACTION_WIDGET_RECEIVER.equals(action)) { 
    if (mp.isPlaying()){ 
      mp.stop(); 
      mp.release(); 
      mp = MediaPlayer.create(context.getApplicationContext(), R.raw.segno); 
    } 
    else{ 
       RemoteViews control = new RemoteViews(context.getPackageName(), R.layout.widget); 
       control.setImageViewResource(R.id.button, R.drawable.pausa); 

       ComponentName cn = new ComponentName(context, Widget.class); 
       AppWidgetManager.getInstance(context).updateAppWidget(cn, control); 

       mp.start(); 

    } 
    super.onReceive(context, intent); 
} 
    super.onReceive(context, intent); 
} 

我的日志本中读到:

02-17 23:46:57.042: W/AppWidgetHostView(946): updateAppWidget couldn't find any view, using error view 

我觉得这是一个问题,但我不知道如何解决它。 当出现这个错误小部件说:问题加载小部件。

谢谢大家

这是一个小部件XML布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:layout_margin="4dp"> 

<Button 
    android:id="@+id/button" 
    android:paddingRight="30dp" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:background="@drawable/vai" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_toRightOf="@+id/button" 
    android:maxHeight="100dp" 
    android:maxWidth="100dp" 
    android:src="@drawable/segno" /> 

</RelativeLayout> 
+0

您可以添加小部件布局吗? – Coderji

+0

当然!添加小部件布局 –

回答

2

你正在试图改变Button的图像,其不按this没有的Button属性setImageViewResource是equivilant到ImageView属性,所以不建议按钮,我建议你将其更改为ImageButtonImageView可点击。

,所以也许你的XML看起来就像这样:

<ImageButton 
    android:id="@+id/button" 
    android:paddingRight="30dp" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:background="@drawable/vai" /> 

更新

的原因是android:backgroundandroid:src它不会重新调整照片尺寸的不同,它实际上使图像尺寸的照片(setImageViewResource与src相同)..所以你必须从头开始调整照片大小或尝试添加此属性到你的ImageButton

android:scaleType="fitXY" 

但我不确定它会工作,还没有尝试过。

希望这适合你。试试看,并给我一个反馈。

+0

酷!有用!!!!谢谢!!!现在的问题是:当图像改变新图像的大小是错误的。 –

+0

请检查更新的部分。如果你发现我的回答很有用,我希望你的答案是正确的。 – Coderji

+0

ops,对不起。我是新来的stackoverflow:P –