开发者

Android widget background

i have an application with a widget. I created some preferences to let the user change the background of my widget. For this i imported 4 .png files to the drawable-hdpi folder. If i'm using LinearLayout in my xml i cannot change the background:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="146dip"
    android:layout_height="72dip"
    android:layout_gravity="center"
    android:开发者_运维百科background="@drawable/goldgreenbg"
    android:id="@+id/widgetlayout">
<TextView android:id="@+id/widget_textview"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textColor="#0B3B0B"
    android:textSize="11sp"/>
<TextView android:id="@+id/widget_textview2"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textSize="12sp"
    android:textColor="#FFFFFF"/>
<TextView android:id="@+id/widget_textview3"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:layout_marginTop="0dip"
    android:padding="0dip"
    android:textSize="9sp"
    android:textColor="#0B3B0B"/>
</LinearLayout>

In EditPreferences.java i set the background like this:

final Preference listpref = getPreferenceScreen().findPreference("listPref");
        listpref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
        {
         public boolean onPreferenceChange(Preference p, Object newValue) 
         {
          String color = (String) newValue;

          if (color.equals("color1"))
          {
              Toast.makeText(EditPreferences.this, "Black", Toast.LENGTH_SHORT);
              RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(215, 215, 215));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(155, 155, 155));
        //    updateViews.setImageViewResource(R.id.ImageView01, R.drawable.blackbg);
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);
          }
          else if (color.equals("color2"))
          {
              Toast.makeText(EditPreferences.this, "Brown", Toast.LENGTH_SHORT);
              RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(23, 81, 11));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(23, 81, 11));
        //    updateViews.setImageViewResource(R.id.ImageView01, R.drawable.brownbg);           
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);
          }
  return true;
         }
        });

As you can see i was using an ImageView in the xml file like this:

<ImageView android:id="@+id/ImageView01"
     android:layout_width="86dip" 
     android:layout_height="72dip"
     android:background="@drawable/pinkwhitebg">
 </ImageView>

But i cannot align it to cover the whole layout unless i use RelativeLayout. Or can I?

If i am using RelativeLayout with the ImageView the imageview is smaller than the image!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="146dip"
    android:layout_height="72dip"
    android:layout_gravity="center"
    android:id="@+id/widgetlayout">
<ImageView android:id="@+id/ImageView01"
     android:layout_width="146dip" 
     android:layout_height="72dip"
     android:layout_marginLeft="3dip"
     android:layout_marginRight="30dip">
 </ImageView>
<TextView android:id="@+id/widget_textview"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:layout_marginLeft="40dip"
    android:paddingLeft="0dip"
    android:textColor="#0B3B0B"
    android:textSize="11sp"/> 
<TextView android:id="@+id/widget_textview2"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:gravity="center_horizontal"
    android:layout_marginTop="0dip"
    android:layout_marginLeft="0dip"
    android:paddingLeft="30dip"
    android:textSize="12sp"
    android:textColor="#E8E86B"
    android:layout_below="@+id/widget_textview"/>
<TextView android:id="@+id/widget_textview3"
    android:text="@string/widget_text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:layout_marginTop="0dip"
    android:layout_marginLeft="0dip"
    android:paddingLeft="30dip"
    android:textSize="9sp"
    android:textColor="#0B3B0B"
    android:layout_below="@+id/widget_textview2"/>
</RelativeLayout>

So it's very strange as i am using the same width and height here as in the linearlayout but the background is thinner..

I've also tried to this very easy method but this way in place of the widget i just get the text "Problem loading widget":

  RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
              updateViews.setTextColor(R.id.widget_textview, Color.rgb(23, 81, 11));
              updateViews.setTextColor(R.id.widget_textview2, Color.WHITE);
              updateViews.setTextColor(R.id.widget_textview3, Color.rgb(23, 81, 11));
              updateViews.setImageViewBitmap(R.id.widgetlayout, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.brownbg)).getBitmap());
              ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
              AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
              manager.updateAppWidget(thisWidget, updateViews);

Question 1: Can i change the background of the widget without using ImageView? How? (this would be the best solution)

Question 2: Can i cover the whole LinearLayout with ImageView?

Question 3: Why is the background image smaller in the ImageView in case of RelativeLayout?

What do you suggest?


Ans 1 : Background of LinearLayout can be set using "android:background" See detail at - http://developer.android.com/reference/android/view/View.html#attr_android:background

Try to set the "android:src" of ImageView instead of "android:background".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜