在 xml 中设置工具栏一半 attr/actionBarSize 的高度

Mos*_*afa 5 android android-layout android-view android-resources android-styles

无论如何要在布局xml文件中设置“android.support.v7.widget.Toolbar”一半的预定义属性的高度,例如“?android:attr/actionBarSize”?事实上,我希望我的工具栏的高度是这样的:

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height=("?android:attr/actionBarSize")/2
    >
Run Code Online (Sandbox Code Playgroud)

azi*_*ian 2

无法在 xml 中执行算术操作。

如果你想通过xml给出值,那么你必须执行以下操作:

  1. 在values/dimes.xml定义一个变量halfActionBar and make it be 28dp (original is 56dp).
  2. 在values-land/dimes.xml定义一个变量halfActionBar and make it be 24dp (original is 48dp).
  3. 定义values-sw600dp-v13/dimes.xml一个变量halfActionBar并将其设置为 32dp(原始为 64dp)。

在你的styles.xml主题中。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionBarSize">@dimen/halfActionBar</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)

然后在你的布局中:

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"/>
Run Code Online (Sandbox Code Playgroud)

请注意,这是一个糟糕的解决方案,因为它取决于平台实现。我建议从 java/kotlin 代码中获取(而不是从 xml 中获取)。actionBarSize