如何在Android的底部放置工具栏?

时间:2016-03-09 08:01:19

标签: android xml android-studio

我喜欢将工具栏放在页面底部。 我的activity_main.xml是

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.gripabb.abbott.gripabb.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_alignParentBottom="true"
            app:popupTheme="@style/AppTheme.PopupOverlay">

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

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</android.support.design.widget.CoordinatorLayout>

然后我的styles.xml是

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

我把这个(android:layout_alignParentBottom="true")放在activity_main.xml

3 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.gripabb.abbott.gripabb.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:layout_alignParentBottom="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

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

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</android.support.design.widget.CoordinatorLayout>

尝试在toolbarview的父布局中使用对齐父底部,对于您的布局,我将其移动到那里。

答案 1 :(得分:0)

将工具栏放在RelativeLayout中。      

   <android.support.v7.widget.Toolbar
      android:id="@+id/topToolbar"
      android:layout_width="fill_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="@color/green">

     <com.jiohealth.patient.views.widgets.AdjustableImageView
         android:id="@+id/iv_back"
        android:layout_width="wrap_content"
         android:layout_height="@dimen/height_36"
         android:layout_gravity="left"
         android:visibility="visible"
         android:adjustViewBounds="true"
         android:padding="@dimen/space_8"
         android:src="@drawable/ic_back_white" />

     <com.jiohealth.patient.views.widgets.AdjustableImageView
         android:id="@+id/iv_video_call"
         android:layout_width="wrap_content"
         android:layout_height="@dimen/height_36"
         android:layout_gravity="right"
         android:visibility="visible"
         android:adjustViewBounds="true"
         android:padding="@dimen/space_8"
         android:src="@drawable/ic_facetime" />

     <com.jiohealth.patient.views.widgets.AdjustableImageView
         android:id="@+id/iv_voice_call"
         android:layout_width="wrap_content"
         android:layout_height="@dimen/height_36"
         android:layout_gravity="right"
         android:visibility="visible"
         android:adjustViewBounds="true"
         android:padding="@dimen/space_8"
         android:src="@drawable/ic_voice_call" />

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

答案 2 :(得分:0)

android:layout_gravity="end|bottom"属性放在AppBarLayout中,如下所示:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

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

    </android.support.design.widget.AppBarLayout>

之后,Toolbar应该在屏幕底部对齐。