Android(安卓)Support Library
16lz
2021-12-04
为什么要使用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; }
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android使用HttpClient和URLConnection获取网页内容
- Android中UI设计的一些技巧!!!
- Android性能调优
- 【Android】TextView动态设置Drawable资源
- versionCode与versionName的区别、应用、获取