为什么要使用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. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android使用HttpClient和URLConnection获取网页内容
  6. Android中UI设计的一些技巧!!!
  7. Android性能调优
  8. 【Android】TextView动态设置Drawable资源
  9. versionCode与versionName的区别、应用、获取

随机推荐

  1. Android(安卓)- 设备的DeviceId
  2. Android之网络请求6————OkHttp源码3:
  3. Android(安卓)Test 基础知识
  4. android camera(四):camera 驱动 GT2005
  5. Android(安卓)自定义view(二) 如何实现自
  6. Android(安卓)Studio主要目录及文件简介
  7. Android(安卓)支持多屏幕机制
  8. android 当前apn的状态以及获取
  9. android中处理json最佳方法
  10. Android(安卓)如何判断CPU是32位还是64位