1. 在attrs.xml中定义ContextMenuPopupWindowAttr以及其属性和类型,即(R.styleable.ContextMenuPopupWindowAttr):
attrs.xml:
<declare-styleable name="ContextMenuPopupWindowAttr">
<attr name="contextmenu_popupwindow_backgroundColor" format="color" />
<attr name="contextmenu_popupwindow_width" format="dimension" />
<attr name="contextmenu_popupwindow_item_height" format="dimension" />
</declare-styleable>
2. 在java类中使用TypedArray:
TypedArray array = context.getTheme().obtainStyledAttributes(R.styleable.ContextMenuPopupWindowAttr);
final int count = array.getIndexCount();int at = 0;
int index = 0;
while (at < count) {
index = array.getIndex(at);
switch (index) {
case R.styleable.ContextMenuPopupWindowAttr_contextmenu_popupwindow_backgroundColor:
mBackgroundColor = array.getColor(index,-1);
break;
case R.styleable.ContextMenuPopupWindowAttr_contextmenu_popupwindow_width:
mWidth = (int)array.getDimension(index,100);
break;
case R.styleable.ContextMenuPopupWindowAttr_contextmenu_popupwindow_item_height:
mItemHeight = (int)array.getDimension(index,100);
break;
}
at ++;
}
array.recycle();
3 . context是Activity,context.getTheme()是指获得对应context的style里面的值,如下:style.xml:
<style name="BrowserTheme" parent="Miui.Theme.Light">
<item name="android:windowBackground">@color/homepage_bg_color</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowDisablePreview">false</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:textSelectHandle">@drawable/miui_text_select_handle_center</item>
<item name="android:textSelectHandleLeft">@drawable/miui_text_select_handle_left</item>
<item name="android:textSelectHandleRight">@drawable/miui_text_select_handle_right</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<!--BrowserPopupWindow-->
<item name="browserpopupwindow_backgroundColor">#c0000000</item>
<item name="browserpopupwindow_padding">@dimen/fragment_padding_pad</item>
<item name="browserpopupwindow_windowAnimationStyle">@style/BrowserPopupWindow.Animation</item>
<!--ContextMenuPopupWindow-->
<item name="contextmenu_popupwindow_backgroundColor">@color/contextmenu_popup_window_bg</item>
<item name="contextmenu_popupwindow_width">@dimen/contextmenu_popupwindow_width</item>
<item name="contextmenu_popupwindow_item_height">@dimen/contextmenu_popupwindow_item_height</item>
</style>
4. 在本例中,contex对应的是BrowserActivity,因此在AndroidManifest.xml中如下定义:
<activity android:name="BrowserActivity"
android:label="@string/application_name"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
android:theme="@style/BrowserTheme"
android:windowSoftInputMode="adjustPan|stateHidden" >