APP开发优化学习笔记1

1、启动白屏、黑屏

给启动页设置Theme,看起来就像秒启动了。


一般启动页也就一张图片,直接设置了android:windowBackground就可以,imageview都不用了。

APP开发优化学习笔记1_第1张图片
Paste_Image.png
APP开发优化学习笔记1_第2张图片
Paste_Image.png

2、ViewStub

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime.
比如加载列表空布局、引导提示等。




    

    


android:layout="@layout/viewstub_layout" ---ViewStub本身被替换掉的布局文件,调用viewStub.inflate()。
android:inflatedId="@+id/viewstub_layout" ---ViewStub被提替换之后的布局的id
替换之后的布局的长宽还是以viewstub的属性为准:
android:layout_width="wrap_content"
android:layout_height="wrap_content"

  • viewstub本身的setVisibility方法,在调用的时候会判断是否inflate(),如果是第一次,则会设置viewstub本身的visibility,并且调用inflate(),当再次调用setVisibility方法的时候,只会设置替换之后的view的visibility,所以viewstub的getVisibility()只会等于第一次设置之后的visibility。
/**
     * When visibility is set to {@link #VISIBLE} or {@link #INVISIBLE},
     * {@link #inflate()} is invoked and this StubbedView is replaced in its parent
     * by the inflated layout resource. After that calls to this function are passed
     * through to the inflated view.
     *
     * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
     *
     * @see #inflate() 
     */
    @Override
    @android.view.RemotableViewMethod
    public void setVisibility(int visibility) {
        if (mInflatedViewRef != null) {
            View view = mInflatedViewRef.get();
            if (view != null) {
                view.setVisibility(visibility);
            } else {
                throw new IllegalStateException("setVisibility called on un-referenced view");
            }
        } else {
            super.setVisibility(visibility);
            if (visibility == VISIBLE || visibility == INVISIBLE) {
                inflate();
            }
        }
    }
APP开发优化学习笔记1_第3张图片
Paste_Image.png

这段代码,viewStub指向的layout会在第一次点击textView的时候显示,然后之后的所有点击都是消失,因为一开始viewStub.getVisibility() == View.GONE,第一次调用的viewStub.setVisibility(View.VISIBLE);之后viewStub.getVisibility()的值就一直等于View.VISIBLE,调用viewStub.setVisibility(View.INVISIBLE)只会控制指向的layout的显示,不会改变viewStub的属性。

  • java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent

对ViewStub的inflate操作只能进行一次,因为inflate的时候是将其指向的布局文件解析inflate并替换掉当前ViewStub本身(由此体现出了ViewStub“占位符”性质),一旦替换后,此时原来的布局文件中就没有ViewStub控件了,因此,如果多次对ViewStub进行infalte,会出现错误信息:ViewStub must have a non-null ViewGroup viewParent。
比如:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textview);
        viewStub = (ViewStub) findViewById(R.id.viewstub);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("viewStub",viewStub.getVisibility() + "");
                if (viewStub.getVisibility() == View.VISIBLE){
                    View view = viewStub.inflate();
                    viewStub.setVisibility(View.INVISIBLE);
                } else {
                    viewStub.setVisibility(View.VISIBLE);
                }
            }
        });

    }

第二次点击textview就会报错,应为第一次调用viewStub.setVisibility(View.VISIBLE)的时候已经调用过一次inflate()。

3、AndroidStudio 便捷操作

Android Studio相见恨晚的操作锦集

你可能感兴趣的:(APP开发优化学习笔记1)