如何根据Material Ratio keylines动态定义AppBarLayout高度

时间:2016-12-09 07:28:36

标签: android material-design android-appbarlayout

通常我会将{strong> AppBarLayout 的高度定义为dp布局中的固定xml。例如;

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height" />

当我详细了解 Google material design 指南时,我逐渐明白绝对比率来定义 AppBarLayout <的宽度和高度(维度) / strong>如下图所示。

要遵循这些宽高比,必须根据物理移动设备的不同屏幕宽度更改 AppBarLayout 的高度。< / p>

因此,我想知道如何动态定义 AppBarLayout 的高度,我应该在哪里定义这些比率定义逻辑(我的意思是callback Activity方法} java class)。

Keyline Matrics for AppBarLayout

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码获取屏幕宽度和高度

DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int width = displaymetrics.widthPixels;

使用以下公式计算应用栏布局高度

int appbar_height = width/1.5;

然后使用

设置Appbar高度
AppBarLayout.LayoutParams layoutParams=new AppBarLayout.LayoutParams(AppBarLayout.LayoutParams.MATCH_PARENT,appbar_height);
AppBarLayout appBarLayout = (AppBarLayout) rootView.findViewById(R.id.appbar);
appBarLayout.setLayoutParams(params);