context.obtainStyledAttributes 研究

我们在自定义View 时,一般都会用到 

TypedArray obtainStyledAttributes(
                AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes);

 对于它的前面2个参数可能大家都知道, 对于后面两个参数一直不明白怎么用。

于是今天查了下。

           context obtainStyledAttributes 最终会调到Resources.Theme.obtainStyledAttributes(AttributeSet set,
                int[] attrs, int defStyleAttr, int defStyleRes) 

再看一下参数解释:

         * @param set The base set of attribute values.  May be null.
         * @param attrs The desired attributes to be retrieved.
         * @param defStyleAttr An attribute in the current theme that contains a
         *                     reference to a style resource that supplies
         *                     defaults values for the TypedArray.  Can be
         *                     0 to not look for defaults.
         * @param defStyleRes A resource identifier of a style resource that
         *                    supplies default values for the TypedArray,
         *                    used only if defStyleAttr is 0 or can not be found
         *                    in the theme.  Can be 0 to not look for defaults.


defStyleAttr 指向当前theme 某个item 描述的style 该style指定了一些默认值为这个TypedArray

defStyleRes  当defStyleAttr 找不到或者为0, 可以直接指定某个style


不是很理解 ,搜了一下源码:

发现用的地方 不多。如CalendarView

ypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.CalendarView,
                R.attr.calendarViewStyle, 0);
CalendarView  在 attrs.xml 中 定义如下:

<declare-styleable name="CalendarView">
        <!-- The first day of week according to {@link Java.util.Calendar}. -->
        <attr name="firstDayOfWeek" format="integer" />
        <!-- Whether do show week numbers. -->
        <attr name="showWeekNumber" format="boolean" />
        <!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->
        <attr name="minDate" />
        <!-- The minimal date shown by this calendar view in mm/dd/yyyy format. -->
        <attr name="maxDate" />
        <!-- The number of weeks to be shown. -->
        <attr name="shownWeekCount" format="integer"/>
        <!-- The background color for the selected week. -->
        <attr name="selectedWeekBackgroundColor" format="color|reference" />
        <!-- The color for the dates of the focused month. -->
        <attr name="focusedMonthDateColor" format="color|reference" />
        <!-- The color for the dates of an unfocused month. -->
        <attr name="unfocusedMonthDateColor" format="color|reference" />
        <!-- The color for the week numbers. -->
        <attr name="weekNumberColor" format="color|reference" />
        <!-- The color for the separator line between weeks. -->
        <attr name="weekSeparatorLineColor" format="color|reference" />
        <!-- Drawable for the vertical bar shown at the beginning and at the end of the selected date. -->
        <attr name="selectedDateVerticalBar" format="reference" />
        <!-- The text appearance for the week day abbreviation of the calendar header. -->
        <attr name="weekDayTextAppearance" format="reference" />
        <!-- The text appearance for the calendar dates. -->
        <attr name="dateTextAppearance" format="reference" />
    </declare-styleable>

默认 defStyleAttr 为 calendarViewStyle 

也定义在attrs.xml

       

 <!-- The CalendarView style. -->
        <attr name="calendarViewStyle" format="reference" />

再看看在theme.xml 中 它指向:

<!-- CalendarView style--Defalut>
        <item name="calendarViewStyle">@style/Widget.CalendarView</item>
        。。。。
           <!-- CalendarView style--Holo>
        <item name="calendarViewStyle">@style/Widget.Holo.CalendarView</item>

        。。。
         <!-- CalendarView style-HOLOLight->
        <item name="calendarViewStyle">@style/Widget.Holo.Light.CalendarView</item>

不同的theme 指向不同的style


 如style.xml 中的


 <style name="Widget.CalendarView">
        <item name="Android:showWeekNumber">true</item>
        <item name="android:minDate">01/01/1900</item>
        <item name="android:maxDate">12/31/2100</item>
        <item name="android:shownWeekCount">6</item>
        <item name="android:selectedWeekBackgroundColor">#330099FF</item>
        <item name="android:focusedMonthDateColor">#FFFFFFFF</item>
        <item name="android:unfocusedMonthDateColor">#66FFFFFF</item>
        <item name="android:weekNumberColor">#33FFFFFF</item>
        <item name="android:weekSeparatorLineColor">#19FFFFFF</item>
        <item name="android:selectedDateVerticalBar">@android:drawable/day_picker_week_view_dayline_holo</item>
        <item name="android:weekDayTextAppearance">@android:style/TextAppearance.Small.CalendarViewWeekDayView</item>
        <item name="android:dateTextAppearance">?android:attr/textAppearanceSmall</item>
    </style>

指定了CalendarView 的atrrs 中的一些默认值。


自此我们明白了defStyleAttr的用法。 如果某个stylable 定义了一些item , 但是使用者并不一定对所有的item 在xml 使用时设置。 我们需要给他设定一些默认值

这些值可以在不同的theme 中不一样。


defStyleRes 使用则更简单。可以直接某个style, 当defStyleAttr不起作用时。


转自:http://blog.csdn.net/yunnywu/article/details/23694341
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml文件里其实是默认的 为了让大家更容易理解,写了一个简单的Demo.步骤如下: 第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast; /** * @author carlshen. * 应用的一些工具类. */ public class ToolUtils { /** * 参数带Context. * @param context * @param msg */ public static void showToast(Context context,String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /** * 调用全局的Context. * @param msg */ public static void showToast(String msg){ Toast.makeText(MainApplication.getContext(), msg, Toast.LENGTH_SHORT).show(); } } 第3步:新建一个View命名为MainView.java就是我们Activity现实的View.代码如下: package com.tutor.application; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; /** * @author carlshen. * 自定义的MainView. */ public class MainView extends FrameLayout implements View.OnClickListener{ private Context mContext; private Activity mActivity; /** * 参数Button. */ private Button mArgButton; /** * 全局Button. */ private Button mGlobleButton; /** * 退出Button. */ private Button mExitButton; public MainView(Context context){ super(context); setupViews(); } public MainView(Context context, AttributeSet attrs) { super(context, attrs); setupViews(); } private void setupViews(){ //获取View的上下文. mContext = getContext(); //这里将Context转换为Activity. mActivity = (Activity)mContext; LayoutInflater inflater = LayoutInflater.from(mContext); View v = inflater.inflate(R.layout.main, null); addView(v); mArgButton = (Button)v.findViewById(R.id.arg_button); mGlobleButton = (Button)v.findViewById(R.id.glo_button); mExitButton = (Button)v.findViewById(R.id.exit_button); mArgButton.setOnClickListener(this); mGlobleButton.setOnClickListener(this); mExitButton.setOnClickListener(this); } public void onClick(View v) { if(v == mArgButton){ ToolUtils.showToast(mContext, "我是通过传递Context参数显示的!"); }else if(v == mGlobleButton){ ToolUtils.showToast("我是通过全局Context显示的!"); }else{ mActivity.finish(); } } } 这里MainView.java使用的布局main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> 第4步:修改ApplicationDemoActivity.java,代码如下: package com.tutor.application; import android.app.Activity; import android.os.Bundle; public class ApplicationDemoActivity extends Activity { private static Context aContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MainView mMainView = new MainView(this); setContentView(mMainView); aContext = getApplicationContext(); } /**获取Context. * @return */ public static Context getContext(){ return aContext; } } 第5步:运行上述工程效果如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值