2011-02-06 3 views
0

위젯 (HelloWidget.java), 활동 (MainActivity.java) 및 listpreference (EditPreferences.java)를 만들었습니다.Android listpreference 및 widget 배경

XML 파일 :

  • main.xml에이 위젯을
  • 의 Config.xml 있습니다 버튼
  • preferences.xml로 :이 활동을 가지고 이것은 listpreference있다

사용자가 위젯의 배경 이미지를 변경할 수 있도록 환경 설정을 만들었습니다. drawable-hdpi 폴더에 4 개의 이미지 파일이 있습니다. 기본 배경 안드로이드 같이 설정된다 : "그리기/goldgreenbg @"= 배경

MainActivity.java에서

나 사용자가 제 1 또는 listpreference 번째 요소를 클릭하면 배경 이미지를 설정하는 코드 가지고

preferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String listpref = preferences.getString("listPref", "n/a");    

    if (listpref.equals("color1")) 
    { 
     Toast.makeText(MainActivity.this, "Black" + listpref, Toast.LENGTH_LONG).show(); 
     setContentView(R.layout.main); 
     LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout); 
     ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg)); 
    } 
    else if (listpref.equals("color2")) 
    { 
     Toast.makeText(MainActivity.this, "Brown" + listpref, Toast.LENGTH_LONG).show(); 
     setContentView(R.layout.main); 
     LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout); 
     ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.brownbg)); 
    } 

불행히도 위젯이 아닌 활동이 변경됩니다. 이제는 위젯이 변경되지 않은 상태에서 버튼이 아닌 배경 이미지가 보입니다. UpdateService.java의 onCreate() 메소드에 이것을 넣으려고했지만 setContentView()를 사용하지 못하게합니다. 아이디어가 있으십니까?

업데이트 : main.xml에 : 해결

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    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="@android:color/white"/> 
<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> 

: 부분은 preferences.java 파일에하고있는 LinearLayout 사용의이 코드 insted해야한다 "경우"

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.setImageViewBitmap(R.id.ImageView01, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.blackbg)).getBitmap()); 
       ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class); 
       AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this); 
       manager.updateAppWidget(thisWidget, updateViews); 

답변

0

당신의 문제가 바로 여기에 있습니다.

LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout); 
ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg)); 

전체 레이아웃 (또는 적어도 LinearLayout 안에있는 모든 것). 대신 위젯을 사용해야합니다.

+0

죄송합니다. 이해가되지 않습니다. main.xml로 메인 포스트를 업데이트했습니다. 위젯입니다. – erdomester

+0

나는 이것을 다음과 같이 시도했습니다 : LinearLayout ll = (LinearLayout) findViewById (R.layout.main); ll.setBackgroundDrawable (getResources(). getDrawable (R.drawable.blackbg)); – erdomester