【问题标题】:Android ActionBar height using a custom layoutAndroid ActionBar 高度使用自定义布局
【发布时间】:2020-10-23 07:00:09
【问题描述】:

我可以在 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 的高度相同。

编辑:

我的布局文件:

<?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>

而且这个布局是在Activity中设置的:

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);
    }
}

【问题讨论】:

    标签: android android-layout android-actionbar


    【解决方案1】:

    在您的样式或布局中添加标准高度,为 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>
    

    【讨论】:

    • 我不想要标准高度。这才是重点。我希望它更短。
    • 很遗憾,这样一来,我的 ActionBar 的高度就和以前一样了。
    • 为我的问题添加了新的编辑
    【解决方案2】:

    在布局资源文件中将父布局高度设置为“match_parent”。

    【讨论】:

      【解决方案3】:

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

      android:layout_marginTop="-8dp"
      

      这会将内容拉上来。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多