【问题标题】:Change AppBarLayout height programmatically in Android在 Android 中以编程方式更改 AppBarLayout 高度
【发布时间】:2015-09-30 11:34:02
【问题描述】:

我正在尝试使用this tutorial 实现具有图像模式的灵活空间。

一切正常。

注意 AppBarLayout 的高度定义为 192dp。

我想改为屏幕的 1/3 高度,以匹配 this google example for the pattern here

这里是activity的onCreate中的代码(布局xml和教程中的一模一样):

AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
float density = getResources().getDisplayMetrics().density;
float heightDp = getResources().getDisplayMetrics().heightPixels / density;
appbar.setLayoutParams(new CoordinatorLayout.LayoutParams(LayoutParams.MATCH_PARENT, Math.round(heightDp / 3)));

但由于某种原因,结果不是我所期望的。这段代码我根本看不到应用栏。 (没有代码,高度按预期显示,但它来自 XML,不能动态设置)。

【问题讨论】:

    标签: java android android-5.0-lollipop material-design appbar


    【解决方案1】:

    改为这样做:

        AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
        float heightDp = getResources().getDisplayMetrics().heightPixels / 3;
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appbar.getLayoutParams();
        lp.height = (int)heightDp;
    

    在您的原始代码中,我认为您对屏幕 1/3 的计算是错误的,但您仍然应该看到一些东西。可能是 setLP() 中的 LayoutParams.MATCH_PARENT 没有正确导入。始终首先声明视图类型,即 CoordinatorLayout.LayoutParams 只是为了确保。否则,例如使用 Framelayout.LayoutParams 会很容易。

    【讨论】:

    • 不应该是这样的吗?:float density = mParentActivity.getResources().getDisplayMetrics().density; float heightDp = mParentActivity.getResources().getDisplayMetrics().heightPixels / 密度;
    【解决方案2】:

    以编程方式改变 AppBarLayout 高度的一些方法,包括屏幕高度的除法、百分比或重量:

    private AppBarLayout appbar;
    
    /**
     * @return AppBarLayout
     */
    @Nullable
    protected AppBarLayout getAppBar() {
        if (appbar == null) appbar = (AppBarLayout) findViewById(R.id.appbar);
        return appbar;
    }
    
    /**
     * @param divide Set AppBar height to screen height divided by 2->5
     */
    protected void setAppBarLayoutHeightOfScreenDivide(@IntRange(from = 2, to = 5) int divide) {
        setAppBarLayoutHeightOfScreenPercent(100 / divide);
    }
    
    /**
     * @param percent Set AppBar height to 20->50% of screen height
     */
    protected void setAppBarLayoutHeightOfScreenPercent(@IntRange(from = 20, to = 50) int percent) {
        setAppBarLayoutHeightOfScreenWeight(percent / 100F);
    }
    
    /**
     * @param weight Set AppBar height to 0.2->0.5 weight of screen height
     */
    protected void setAppBarLayoutHeightOfScreenWeight(@FloatRange(from = 0.2F, to = 0.5F) float weight) {
        if (getAppBar() != null) {
            ViewGroup.LayoutParams params = getAppBar().getLayoutParams();
            params.height = Math.round(getResources().getDisplayMetrics().heightPixels * weight);
            getAppBar().setLayoutParams(params);
        }
    }
    

    如果您想遵循材料设计指南,高度应等于默认高度加上内容增量 https://www.google.com/design/spec/layout/structure.html#structure-app-bar

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 2016-04-17
      • 2023-01-17
      • 1970-01-01
      • 2016-10-25
      • 2014-01-22
      • 2017-04-25
      • 2014-07-05
      相关资源
      最近更新 更多