【问题标题】:Add custom layout to toolbar将自定义布局添加到工具栏
【发布时间】:2015-09-25 04:20:43
【问题描述】:

我按照tutorial 为我的应用程序提供了一个带有一些选项卡的常规工具栏。

我想更改工具栏,使其看起来更像这样:

我想在工具栏中添加一些文本和图像。我该怎么做?

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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ToolbarTheme"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextAppearance="@style/TabLayout"
            app:tabSelectedTextColor="@color/white"
            />

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

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

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

【问题讨论】:

  • 这是一个单行工具栏,其他信息只是具有相同背景颜色的线性布局。您也可以在工具栏中添加视图,只需添加一个结束标签&lt;android.support.v7.widget.Toolbar&gt;&lt;/android.support.v7.widget.Toolbar&gt;
  • 如果我在工具栏标签中添加布局,行为会改变吗?如果工具栏的高度为 500 像素怎么办?
  • @Elltz 这与底部工具栏完全无关。这与修改Android提供的Toolbar有关。
  • 它就像一个线性布局,它根据工具栏内的视图改变高度

标签: android android-layout android-xml


【解决方案1】:

你可以试试这个:-

<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_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


        <!-   Your view --> 


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="6dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">


      <android.support.v7.widget.Toolbar
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:id="@+id/toolbar"
         android:layout_height="?attr/actionBarSize"
         android:elevation="6dp"
         app:layout_collapseMode="pin"
         app:layout_scrollFlags="scroll|enterAlways"
         app:popupTheme="@style/AppTheme">

           <LinearLayout
               android:id="@+id/llContainer"
               android:layout_width="match_parent"
               android:orientation="vertical"
               android:background="@android:color/black"
               android:gravity="center"
               android:layout_height="300dp">

                 <!-  Your TextView / ImageView -->

            </LinearLayout>

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

      <android.support.design.widget.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabTextAppearance="@style/TabLayout"
          app:tabSelectedTextColor="@color/white"/>

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

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

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

【讨论】:

    【解决方案2】:

    您可以使用约束布局定义 ToolBar 自定义布局,如下所示,请参阅http://www.zoftino.com/android-toolbar-tutorial 了解更多信息。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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"
        tools:context="zoftino.com.toolbar.MainActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dp">
                <TextView
                    android:id="@+id/title_m"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="16dp"
                    android:text="Tennis"
                    android:textAppearance="@android:style/TextAppearance.Material.Title"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>
                <TextView
                    android:id="@+id/women_s"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:text="W Singles"
                    android:textAppearance="@android:style/TextAppearance.Material.Medium"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/title_m" />
                <TextView
                    android:id="@+id/wm_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:textAppearance="@android:style/TextAppearance.Material.Title"
                    android:text="Serena"
                    app:layout_constraintLeft_toRightOf="@+id/women_s"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/title_m" />
    
            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.Toolbar>
    
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案3】:

      Android ActionBar 现在比以往任何时候都更容易定制,因为旧的 ActionBar 元素已被更通用的工具栏取代。更多detail reference link 1detail reference link 2

      【讨论】:

      • 这是一个示例,说明答案应该显示链接中的重要内容,以防链接停止工作。
      猜你喜欢
      • 2011-06-27
      • 2015-10-18
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多