android – setElevation()无法在AppBarLayout上运行

我有一个AppBarLayout,我希望在onCreate()方法中以编程方式将高程设置为0.如果我有一个像这样的onCreate()示例..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(_toolbar);
    _appBar.setElevation(0);
}

AppBarLayout提升不会改变.

但是,如果我添加延迟,例如400毫秒,则AppBarLayout高程会发生变化.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    setSupportActionBar(_toolbar);
        Observable.just(_appBar)
        .delay(400, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<AppBarLayout>() {
            @Override
            public void accept(AppBarLayout appBarLayout) throws Exception {
                appBarLayout.setElevation(0);
            }
        });
}

任何人都可以解释为什么会这样吗?以及我如何能够毫不拖延地解决这个问题.我也尝试过使用getSupportActionBar().setElevation(0);同样,并使用API​​ 24

解决方法:

解决了!而不是设置高程,我使用:

StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
appBarLayout.setStateListAnimator(stateListAnimator);

有关更详细的说明,请参阅以下答案:

How to set the elevation of an AppBarLayout programmatically in the Android Support Library v24.0.0?

上一篇:android – 如何像Lollipop中的Contacts应用程序一样制作工具栏动画


下一篇:Pycharm toolbar 工具栏添加扩展