AppCompatView

AppCompatTextView,AppCompatImageView等等AppCompatXxView是在support v7中引入的,可以使用高版本的才有的一些特性,比如tint着色的部分功能,是在L开始才有的,使用AppCompatView之后可以向低版本兼容。

当我们的Activity继承自AppCompatActivity时,内部自动会将我们在布局中正常View对象转换为对应的AppCompatView对象来使用。

一般继承XXView时都会提示继承AppCompatXxView,而不是直接继承Framework中自带的View,AppCompatView都是继承自对应Framework中对应的View,只是内部做了一些特殊处理。

AppCompatActivity ->AppCompatDelegate ->AppCompatDelegateImplV9
AppCompatViewInflater.java

    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 it's android:onClick
            checkOnClickListener(view, attrs);
        }

        return view;
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,034评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,487评论 4 61
  • 原文地址:http://www.android100.org/html/201606/06/241682.html...
    AFinalStone阅读 4,844评论 0 1
  • 我未曾用悲伤染指的土地是我的青春,那里面有懵懂,有彷徨,有热情,也有欢乐,唯独没有悲伤。 青春像一条小溪,它流在我...
    果果1986阅读 1,562评论 0 0
  • 猎豹移动CEO傅盛在一次演讲中曾说过:“认知是人类史上取得进步的唯一武器”。 正是认知水品的高低,决定了人类在进化...
    破壁人Jack阅读 4,127评论 4 5

友情链接更多精彩内容