使用自定义布局的Android ActionBar高度

问题描述

我可以在 styles.xml 资源文件中设置ActionBar的高度:

 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarSize">40dp</item>
</style>

这样,我的操作栏会比认操作栏小一些。但是在ActionBar下面会出现一个白色的缝隙。内容从与ActionBar具有相同高度的相同位置开始。

enter image description here

编辑:

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainMenuLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top">

    <FrameLayout
        android:id="@+id/mmContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/mmBottomNavigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/mmBottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation_menu"
        app:itemBackground="@color/white"
        app:itemIconTint="@color/btn_nav_itam_color"
        app:itemTextColor="@color/btn_nav_itam_color" />

</RelativeLayout>

编辑2: 我在一个空白的应用程序上尝试过。 actionBarSize属性按预期方式工作。也没有白缝。

我的应用为操作栏使用自定义布局- custom_action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="0dp" android:layout_margin="0dp"
    android:background="@color/colorPrimary" >

    <TextView
        android:id="@+id/titleTvLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="@string/app_name"
        android:textColor="@color/actionBarText"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/titleTvRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:text="@string/title_bar_loggedin"
        android:textColor="@color/actionBarDarkText"
        android:textSize="12sp"
        android:textStyle="bold" />
</RelativeLayout>

此布局是在“活动”中设置的:

public static void setCustomTitle(AppCompatActivity apc,String userName) {
    if (apc.getSupportActionBar() != null) {
        apc.getSupportActionBar().setdisplayOptions(ActionBar.disPLAY_SHOW_CUSTOM);
        apc.getSupportActionBar().setdisplayShowCustomEnabled(true);
        apc.getSupportActionBar().setCustomView(R.layout.custom_action_bar);
        View view = apc.getSupportActionBar().getCustomView();
        TextView tvLeft = (TextView) view.findViewById(R.id.titleTvLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.titleTvRight);
        tvLeft.setText(apc.getResources().getString(R.string.app_name));
        String rightTitle = apc.getResources().getString(R.string.title_bar_loggedin) + " " + userName;
        tvRight.setText(rightTitle);
    }
}

解决方法

在样式或布局中添加标准高度,为style.xml添加示例

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:actionBarStyle">@style/CustomActionBar</item>
</style>

 <style name="CustomActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">  
    <item name="android:actionBarSize">40dp</item>
    <item name="android:background">@color/colorPrimary</item>
</style>
,

在布局资源文件中将父布局高度设置为“ match_parent”。 the resource file of the activity

,

这是一个技巧,但是对于内容区域的父容器,您可以添加-8dp的最高边距。

android:layout_marginTop="-8dp"

这将拉起内容。