TypedArray 与 常量数组一起使用

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" >












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值