为什么要使用Android Support Library

App编译时用的android sdk(android.jar)不会打包进我们的App中,包括我们使用的控件,因为android.jar中的接口就是android设备里系统框架层(framework)对外提供的接口。

由于有些控件在Android低版本上不存在,为了使低版本系统也能使用高版本控件,官方把一些控件放到Android Support Library中,使用Android Support Library让我们免于判断系统版本,还可以使App在不同版本保持同样的用户体验,在App开发过程中要尽量使用支持库控件。

AppCompatActivity

AppCompatActivity中通过AppCompatDelegate来扩展Activity,AppCompatDelegate使用createView方法创建View时会将某些系统控件转换为AppCompat控件。

所以当我们的控件不在AppCompatActivity里Inflater时需要手动使用支持库控件(Fragment内的控件取决于它的Activity是否是AppCompatActivity),例如自定义View。使用不会被转换的控件时也需要手动使用,例如android.support.v7.widget.CardView。

public final View createView(View parent, final String name, @NonNull Context context,            @NonNull AttributeSet attrs, boolean inheritContext,            boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {        final Context originalContext = context;        // We can emulate Lollipop's android:theme attribute propagating down the view hierarchy        // by using the parent's context        if (inheritContext && parent != null) {            context = parent.getContext();        }        if (readAndroidTheme || readAppTheme) {            // We then apply the theme on the context, if specified            context = themifyContext(context, attrs, readAndroidTheme, readAppTheme);        }        if (wrapContext) {            context = TintContextWrapper.wrap(context);        }        View view = null;        // We need to 'inject' our tint aware Views in place of the standard framework versions        switch (name) {            case "TextView":                view = new AppCompatTextView(context, attrs);                break;            case "ImageView":                view = new AppCompatImageView(context, attrs);                break;            case "Button":                view = new AppCompatButton(context, attrs);                break;            case "EditText":                view = new AppCompatEditText(context, attrs);                break;            case "Spinner":                view = new AppCompatSpinner(context, attrs);                break;            case "ImageButton":                view = new AppCompatImageButton(context, attrs);                break;            case "CheckBox":                view = new AppCompatCheckBox(context, attrs);                break;            case "RadioButton":                view = new AppCompatRadioButton(context, attrs);                break;            case "CheckedTextView":                view = new AppCompatCheckedTextView(context, attrs);                break;            case "AutoCompleteTextView":                view = new AppCompatAutoCompleteTextView(context, attrs);                break;            case "MultiAutoCompleteTextView":                view = new AppCompatMultiAutoCompleteTextView(context, attrs);                break;            case "RatingBar":                view = new AppCompatRatingBar(context, attrs);                break;            case "SeekBar":                view = new AppCompatSeekBar(context, attrs);                break;        }        if (view == null && originalContext != context) {            // If the original context does not equal our themed context, then we need to manually            // inflate it using the name so that android:theme takes effect.            view = createViewFromTag(context, name, attrs);        }        if (view != null) {            // If we have created a view, check its android:onClick            checkOnClickListener(view, attrs);        }        return view;    }

更多相关文章

  1. 自定义SeekBarPreference控件(老外出品,直接在preferences文件中
  2. Android中如何修改系统时间
  3. Android基础控件——ProgressBar自定义的介绍、动画效果实现、附
  4. Android系统编译
  5. 1.1.2 Android的系统框架
  6. android studio开发安卓应用设置版本号
  7. android各个版本的代号
  8. Android P系统设置之默认选择网络类型(4G,3G,2G)

随机推荐

  1. 【QQ登录】Android_SDK使用说明
  2. 2018-07-23
  3. Android(安卓)----制作自己的Vendor
  4. Android 解压文件包
  5. 《Android 基础(十二)》 TextInputLayout,让
  6. android下播放器视频输出方法总结
  7. android studio 打包release出现的错误
  8. Android在外部修改了工程名文件夹名称,报
  9. Android(安卓)service与线程区别
  10. [CSDN]Android应用程序启动过程源代码分