【问题标题】:Changing ActionBar Height更改操作栏高度
【发布时间】:2022-01-24 06:27:14
【问题描述】:

我用这个改变了themes.xml中的ActionBar高度

<item name="actionBarSize">30dp</item>

ActionBar 的高度发生了变化,但随后它使我的第一个控件的对齐方式失效。

在改变 ActionBar 高度之前:

更改操作栏高度后

第一个 Spinner 控件与 Layout 的顶部对齐

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.MainFragment">



<Spinner
    android:id="@+id/spSessionsMain"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="@drawable/background_spinner"
    android:spinnerMode="dropdown"
    app:layout_constraintLeft_toRightOf="parent"
    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    />

我完全不知所措。

【问题讨论】:

    标签: android kotlin android-layout


    【解决方案1】:

    发生这种情况是因为操作栏在其样式中默认定义的最小高度为 64dp:

    ActionBar 样式:(仅供参考,请勿复制粘贴到您的主题中)

    <style name="Widget.MaterialComponents.Light.ActionBar.Solid" parent="Widget.AppCompat.Light.ActionBar.Solid">
      <item name="titleTextStyle">?attr/textAppearanceHeadline6</item>
      <item name="subtitleTextStyle">?attr/textAppearanceSubtitle1</item>
      <!-- Overrides minimum height in landscape to avoid headline6 and subtitle1 height concerns. -->
      <item name="android:minHeight">@dimen/mtrl_toolbar_default_height</item>
      <item name="maxButtonHeight">@dimen/mtrl_toolbar_default_height</item>
    </style>
    

    如果您想消除微调器和操作栏之间的间隙,您可以覆盖操作栏的最小高度。像这样:

    <style name="CustomActionBar" parent="Widget.MaterialComponents.Light.ActionBar.Solid">
       <item name="android:minHeight">30dp</item>
    </style>
    

    然后将这一行添加到您的主题中:

    <item name="actionBarStyle">@style/CustomActionBar</item>
    

    【讨论】:

    • 不工作。我正在使用 Theme.MaterialComponents.DayNight.DarkActionBar '
    • 我想通了。我将我的主题更改为 Theme.MaterialComponents.DayNight.DarkActionBar.Bridge 然后使用 35dp 没有更多的间隙
    • @Beachdog 它也应该适用于 DayNight.DarkActionBar 主题,因为它从 Light.ActionBar 主题继承了 actionBarStyle。我试过了,它还在工作。除非您正在做其他事情,否则您不需要使用桥接器。
    • @Beachdog 你能告诉我你到底做了什么吗?你在你的themes.xml中粘贴了什么代码,什么没有?
    • 创建了一个新样式 然后在themes.xml中我引用了新样式 @style/CustomActionBar 这导致ActionBar只是空白
    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多