簡體   English   中英

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

[英]Android ActionBar height using a custom layout

我可以在我的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);
    }
}

在您的樣式或布局中添加標准高度,為 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”。 活動的資源文件

這是一個技巧,但對於內容區域的父容器,您可以添加 -8dp 的上邊距。

android:layout_marginTop="-8dp"

這將拉起內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM