Android的名单preference和widget背景 [英] Android listpreference and widget background
问题描述
我创建了一个小部件(HelloWidget.java),因为它(MainActivity.java)的活动和一个列表preference(编辑preferences.java)。
XML文件:
- main.xml中:这个过程有一个小部件
- 的Config.xml:这有活动:按钮
- preferences.xml:这个过程有一个列表preference
我创造了preferences让用户改变控件的背景图像。我在绘制 - 华电国际文件夹,这4图像文件。默认背景设置Android这样的:背景=@绘制/ goldgreenbg
在MainActivity.java我有这个code。如果用户点击第一或列表preference的第二个元素设置背景图片:
preferences = preferenceManager.getDefaultShared preferences(本);
字符串列表preF = preferences.getString(名单preF,N / A); 如果(名单pref.equals(颜色1))
{
Toast.makeText(MainActivity.this,黑名单+ preF,Toast.LENGTH_LONG).show();
的setContentView(R.layout.main);
LL的LinearLayout =(的LinearLayout)findViewById(R.id.widgetlayout);
ll.setBackgroundDrawable(getResources()getDrawable(R.drawable.blackbg));
}
否则,如果(名单pref.equals(颜色2))
{
Toast.makeText(MainActivity.this,布朗+列表preF,Toast.LENGTH_LONG).show();
的setContentView(R.layout.main);
LL的LinearLayout =(的LinearLayout)findViewById(R.id.widgetlayout);
ll.setBackgroundDrawable(getResources()getDrawable(R.drawable.brownbg));
}
不幸的是这会导致在改变的活性,而不是小部件。所以,现在我看到的背景图像,而不是按钮的活动,而小部件是不变的。
我试图把这个UpdateService.java的onCreate()方法,但它不启用的setContentView()来使用。
任何想法?
更新:
main.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:背景=@绘制/ goldgreenbg
机器人:ID =@ + ID / widgetlayout>
< TextView的机器人:ID =@ + ID / widget_textview
机器人:文字=@字符串/ widget_text
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_gravity =CENTER_HORIZONTAL |中心
机器人:重力=CENTER_HORIZONTAL
机器人:layout_marginTop =0dip
机器人:填充=0dip
机器人:文字颜色=#0B3B0B
机器人:TEXTSIZE =11SP/>< TextView的机器人:ID =@ + ID / widget_textview2
机器人:文字=@字符串/ widget_text
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_gravity =CENTER_HORIZONTAL |中心
机器人:重力=CENTER_HORIZONTAL
机器人:layout_marginTop =0dip
机器人:填充=0dip
机器人:TEXTSIZE =12SP
机器人:文字颜色=@机器人:彩色/白/>
< TextView的机器人:ID =@ + ID / widget_textview3
机器人:文字=@字符串/ widget_text
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_gravity =CENTER_HORIZONTAL |中心
机器人:layout_marginTop =0dip
机器人:填充=0dip
机器人:TEXTSIZE =9SP
机器人:文字颜色=#0B3B0B/>
< / LinearLayout中>
解决:
如果部分应该是在preferences.java文件,insted的的LinearLayout利用这个code:
RemoteViews updateViews =新的RemoteViews(编辑preferences.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)编辑preferences.this.getResources()getDrawable(R.drawable.blackbg))getBitmap());
组件名thisWidget =新的组件名(编辑preferences.this,HelloWidget.class);
AppWidgetManager经理= AppWidgetManager.getInstance(编辑preferences.this);
manager.updateAppWidget(thisWidget,updateViews);
您的问题就在这里:
的LinearLayout LL =(的LinearLayout)findViewById(R.id.widgetlayout);
ll.setBackgroundDrawable(getResources()getDrawable(R.drawable.blackbg));
您设置整个布局(或至少是任何内部的LinearLayout)。你应该用的小部件来代替。
I created a widget (HelloWidget.java), an activity for it (MainActivity.java) and a listpreference (EditPreferences.java).
XML files:
- Main.xml: this has the widget
- Config.xml: this has the activity: buttons
- preferences.xml: this has the listpreference
I created the preferences to let the user change the background image of the widget. I have 4 image files for this in the drawable-hdpi folder. Default background is set like android:background="@drawable/goldgreenbg"
In MainActivity.java i have this code to set the background image if user clicks the first or the second element of the 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));
}
Unfortunately this results in changing the activity, not the widget. So now i see the background image instead of the buttons in the activity while the widget is unchanged. I tried to put this in the onCreate() method of UpdateService.java but it doesn't enable setContentView() to use. Any ideas?
Update: 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>
Solved: The "If" part should be in the preferences.java file and insted of linearlayout use this code:
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);
Your problem is right here:
LinearLayout ll = (LinearLayout) findViewById(R.id.widgetlayout);
ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.blackbg));
Your setting the whole layout (or at least whatever is inside LinearLayout). You should be using the widget instead.
这篇关于Android的名单preference和widget背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!