Extended Toolbar with Custom View not Displaying with Full Width

后端 未结 3 1948
眼角桃花
眼角桃花 2021-02-03 12:45

I went through lots of answers here related to Toolbar but none of the answers could help me.

What I\'m trying to achieve is to have an extended toolbar whhich will disp

相关标签:
3条回答
  • 2021-02-03 13:07

    From the Toolbar documentation:

    One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.

    So from what it look like, your custom view is behaving as it should. The options menu is taking up a certain amount of space in your toolbar, so the Toolbar is measuring the remaining space for your custom view to fit in.

    0 讨论(0)
  • 2021-02-03 13:20

    So in order to achieve this and have complete control over the padding in the toolbar I created two toolbars. The first toolbar with standard height of 56dp and second with height of 72dp which together made a double layered toolbar as specified by material design.

    And because I am not inflating any menu items in the second toolbar all my cusotm views inside behave as desired.

    These lines still need to be used though

        app:contentInsetStart="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        android:clipToPadding="false"
    

    This solved my issue so now I'm including two toolbars into my XMl.

    0 讨论(0)
  • 2021-02-03 13:30

    since versions 23 of the design support libraries there is a much simpler way to do it using android.support.design.widget.CoordinatorLayout

    This is an example:

    <android.support.design.widget.CoordinatorLayout 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"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:popupTheme="@style/AppTheme.PopupOverlay">            
            </android.support.v7.widget.Toolbar>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/project_light_green"
                android:orientation="horizontal"
                android:paddingLeft="@dimen/activity_horizontal_margin_half"
                android:paddingRight="@dimen/activity_horizontal_margin_half"
                android:paddingTop="@dimen/activity_vertical_margin_half">
    
                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_margin="12dp"
                    android:src="@drawable/ic_search_24dp" />
    
                <EditText
                    android:id="@+id/search_field"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="@null"
                    android:hint="@string/autocomplete_hint"
                    android:singleLine="true"
                    android:textColor="@color/project_black"
                    android:textColorHint="@color/project_dark_gray"/>
    
                <include layout="@layout/close_button"
                    android:id="@+id/clearButton"/>
    
            </LinearLayout>
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_search_location" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    0 讨论(0)
提交回复
热议问题