【问题标题】:custom toolbar not visible in fragment自定义工具栏在片段中不可见
【发布时间】:2018-06-24 17:00:51
【问题描述】:

我正在使用自定义工具栏。但它没有按要求显示正确的结果。在我的设计视图中,它显示得很好。但是当我运行我添加的应用程序搜索栏时没有显示。按照屏幕截图了解更多详情。

这是我需要的输出。在我的设计视图中它显示相同。

header_home.xml

<?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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:id="@+id/header_home1"
        android:gravity="center_vertical"
        android:orientation="vertical">

    <ImageView
        android:id="@+id/header_home_iv_header_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:contentDescription="@null" />

    <LinearLayout
        android:id="@+id/header_home_ll_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/dimen_10"
        android:layout_marginTop="@dimen/dimen_10"
        android:gravity="center_vertical">

        <ImageButton
            android:id="@+id/header_ib_menu"
            style="@style/ibHeader"
            android:contentDescription="@null"
            android:minWidth="@dimen/dimen_50"
            android:src="@drawable/ic_menu" />

        <!--android:background="@drawable/bg_search_header"-->


        <ImageButton
            android:id="@+id/header_ib_my_cart"
            style="@style/ibHeader"
            android:layout_marginStart="@dimen/dimen_5"
            android:contentDescription="@null"
            android:src="@drawable/ic_cart_white" />

        <ImageButton
            android:id="@+id/header_ib_notification"
            style="@style/ibHeader"
            android:contentDescription="@null"
            android:src="@drawable/ic_notification_white" />

    </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:gravity="center_vertical"
        android:layout_below="@+id/header_home1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/header_home_iv_header_bg2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:background="@color/colorPrimaryDark"
            android:contentDescription="@null" />

        <LinearLayout
            android:id="@+id/header_home_ll_search"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="@drawable/bg_search_header"
            android:gravity="center_vertical"
            android:orientation="horizontal">


            <ImageButton
                android:id="@+id/header_ib_search"
                style="@style/ibHeader"
                android:clickable="false"
                android:contentDescription="@null"
                android:padding="@dimen/dimen_10"
                android:src="@drawable/ic_search_white" />

            <com.lessohome.customview.CustomFontTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:shadowColor="@color/colorFontDarkGray"
                android:shadowDx="1"
                android:shadowDy="1"
                android:shadowRadius="2"
                android:text="@string/search_for_products"
                android:textColor="@color/colorWhite"
                android:textSize="@dimen/font_14"
                app:fontText="@string/Muli_Bold" />

            <!--android:shadowColor="@color/colorFontDarkGray"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"-->

            <ImageButton
                android:id="@+id/header_ib_qr_code_scan"
                style="@style/ibHeader"
                android:contentDescription="@null"
                android:padding="@dimen/dimen_10"
                android:src="@drawable/ic_scan_white" />


        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

当我运行应用程序时,它只显示工具栏的这么多部分。即使在我的 fragment_home.xml 中,在设计视图中也显示相同。搜索栏未显示。

fragment_home.xml

<android.support.v7.widget.Toolbar
        android:id="@+id/fragment_home_tb_toolbar"
        style="@style/toolbarStyle"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:elevation="@dimen/dimen_4"
        android:fitsSystemWindows="false">

        <include
            android:id="@+id/fragment_home_header"
            layout="@layout/header_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v7.widget.Toolbar>

谁能帮我解决这个问题,我无法找到我做错了什么。

【问题讨论】:

    标签: android android-layout android-fragments android-actionbar android-toolbar


    【解决方案1】:

    工具栏的高度和其他相关布局相同,即 android:layout_height="?android:attr/actionBarSize"

    这就是为什么只有标题布局可见而不是搜索布局,因为标题布局占据了工具栏的整个高度,而搜索布局在标题布局下方。 改变工具栏的高度,你会得到想要的结果。

    【讨论】:

    • 要改变什么?
    • 将工具栏的高度更改为 android:attr/actionBarSize 的两倍 android:attr/actionBarSize 默认值 = 56dp 取决于设备分辨率。所以将工具栏高度设置为 112dp。
    • 我将其更改为包装内容,现在它可见但现在我面临滚动问题..我的 viewpager 不可见..
    • @arvid 我根据我的要求更改了布局现在的问题是,我的工具栏覆盖了 viewpager 图像的一半视图。现在可以看到一半图像我想要完全可见。你能帮忙吗
    • @chris 将 viewpager 保留在工具栏下方。如果您可以显示 viewpager XML 文件,这将非常有帮助。没有看到xml文件我帮不了你。
    【解决方案2】:

    搜索视图的 layout_below 应该是

    android:layout_below="@id/header_home1"
    

    而不是

    android:layout_below="@+id/header_home1"
    

    【讨论】:

      【解决方案3】:

      给出工具栏的高度

      android:layout_height="wrap_content"

      而不是

      android:layout_height="?android:attr/actionBarSize"

      【讨论】:

      猜你喜欢
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      相关资源
      最近更新 更多