【问题标题】:setElevation() not working on AppBarLayoutsetElevation() 不适用于 AppBarLayout
【发布时间】:2017-07-09 14:24:35
【问题描述】:

我有一个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

【问题讨论】:

    标签: android toolbar android-appcompat


    【解决方案1】:

    解决了!而不是设置海拔,我使用:

    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?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      相关资源
      最近更新 更多