1、android setTheme调用在代码中无效的解决
问题的现象:
在代码中直接调用
setTheme(R.style.them1);
之类的没有任何效果
例如:
public class Activity2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTheme(R.style.them1);
}
}
初始化任何View之前设置主题。比如,在调 用setContentView(View) 和inflate(int, ViewGroup)方法前。这保证系统将当前主题应用在所有的UI界面。
解决方案:
但是只要把setTheme放在setContentView之前便可以看到效果(详见http://site.douban.com/widget/notes/110027/note/120907913/)
在此基础上,作者进行了改进
代码如下:
public class ThemeTestActivity extends Activity {
/** Called when the activity is first created. */
int q = 1;
Button button;
Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
// setTheme(R.style.them1);
super.onCreate(savedInstanceState);
setTheme(R.style.them1);//调用重写的setTheme方法
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
System.out.println(getTheme().toString());
button2 = (Button) findViewById(R.id.button2);
onClinc();
}
//这里的代码将要被回调
@Override
public void setTheme(int resid) {
System.out.println("调用setTheme" + resid);
System.out.println("q=>>>>>" + q);
if (q == 1) {
super.setTheme(resid);
} else if (q == 2) {
super.setTheme(R.style.them1);
} else if (q == 3) {
super.setTheme(R.style.them2);
}
}
/**
* 对 Button的事件设置
*/
private void onClinc() {
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
q = 2;
System.out.println("Button1>>>");
onCreate(null);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// setTheme(R.style.them2);
q = 3;
System.out.println("Button2>>>");
onCreate(null);
// setContentView(R.layout.main);
}
});
}
}
请认真看代码执行的流程
参考:http://endf.iteye.com/blog/771685
http://slashgill.blogspot.com/2011/08/activity-settheme.html
2、android 主题的中?号的使用
例如:代码的调用
<TextView
android:id="@id/btnPrev"
style="@style/detail_btn_left"
android:background="?bg_btn_button_bl"
android:drawableTop="@drawable/toolbar_prev_icon"
android:text="上一题" />
请看 android:background="?bg_btn_button_bl" 这个属性将要调用在Theme中声明
见下:
<style name="blueSummerTheme" parent="@style/MyTheme">
<item name="android:windowBackground">@color/bg_Blue</item>
<item name="android:windowTitleBackgroundStyle">@style/MyTitleBarSummer</item>
<item name="android:expandableListViewStyle">@style/MyExpandableListViewSummer</item>
<item name="android:gridViewStyle">@style/MyGridViewSummer</item>
<item name="android:listViewStyle">@style/MyListViewSummer</item>
<item name="base_bg_colora">@color/bg_Blue</item>
<item name="base_bg_colorb">@color/bg_BlueB</item>
<item name="base_bg_colorc">@color/bg_BlueB</item>
<item name="title_text">@color/white</item>
<item name="title_button_text">@color/white</item>
<item name="bg_btn_button">@drawable/bg_btn_button_1</item>
<item name="bg_btn_toggle">@drawable/bg_btn_toggle_1</item>
<item name="bg_btn_button_bl">@drawable/bg_btn_button_tbl_1</item>
<item name="bg_btn_button_bm">@drawable/bg_btn_button_tbm_1</item>
<item name="bg_btn_button_br">@drawable/bg_btn_button_tbr_1</item>
</style>
关于Android的问号?和@符号的用法
http://www.cnblogs.com/playing/archive/2011/04/01/2002469.html