Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect behavior of gravity attributes of new Android Toolbar's child views

I am trying to implement custom layout inside new Toolbar widget provided by new AppCompat library according to this sketch

enter image description here

So I am trying to add new Layout component inside Toolbar and set it's gravity attributes to bottom|center_horizontal. But it seems like this attribute completely doesn't matter and my child layout always positioned on the vertical center of Toolbar and to left of hamburger icon.

enter image description here

My XML-layout looks like this:

<LinearLayout
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="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="115dp"
    android:background="?attr/colorPrimary"
    app:titleTextAppearance="@style/title">

    <RelativeLayout
        android:layout_width="176dp"
        android:layout_height="59dp"
        android:background="?attr/colorPrimaryDark"
        android:gravity="bottom|center_horizontal">

    </RelativeLayout>

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


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ListView
        android:id="@+id/drawer_menu"
        android:layout_width="264dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        android:choiceMode="singleChoice"
        android:divider="?android:colorBackground"
        android:dividerHeight="1dp"/>

</android.support.v4.widget.DrawerLayout>

Any ideas how to achieve correct alignment of child views inside Toolbar?

like image 605
Oleg Osipenko Avatar asked Dec 15 '22 18:12

Oleg Osipenko


1 Answers

EDIT: android:gravity defines how to align the inner content of the view (text alignment, child views, etc.), not the alignment of the view in its parent view.

change

android:gravity="bottom|center_horizontal" 

to

android:layout_gravity="bottom|center_horizontal"
like image 136
Dmitry Avatar answered Apr 06 '23 06:04

Dmitry