自定义 Activity 的 标题栏 TitleBar
1. 修改标题栏的高度,背景
编辑styles.xml,添加:
<?xmlversion="1.0" encoding="utf-8"?> <resources> <style name="titlebarstyle"parent="android:Theme"> <item name="android:windowTitleSize">38dip</item> <item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackgroundColor</item> </style> <style name="CustomizedWindowTitleBackgroundColor"> <item name="android:background">#047BF0</item> </style> </resources>
2. 替换titleBar的布局
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
//一定要按这个调用顺序才行,否则不起作用 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); }
R.layout.titlebar 是自定义的布局
自定义activity时的另外一种写法:
@Override protected void requestWindowFeature(Intent intent) { requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); }
@Override
public void setContentView(int layoutResID) { super.setContentView(layoutResID); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_commen_title); }
normal
no_title
custom_title
1111111111111111111111111
22222222222222222222
33333333333333