【问题标题】:toolbar background doesn't adjust and title is not shown - android工具栏背景不调整,标题不显示 - android
【发布时间】:2016-11-29 01:13:14
【问题描述】:

我正在尝试创建一个使用我在 png 文件中制作的背景的工具栏。 现在,当我在 xml 中使用背景时,android studio 中的布局编辑器完全显示了预期的结果。当我在虚拟设备中运行应用程序时,问题就来了。背景不会根据工具栏进行调整,因此只会显示其中的一部分。

此外,标题和物品表现得很奇怪。标题根本不显示,工具栏中唯一显示的项目突然跳到左边。

工具栏的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_bar_styled"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:elevation="15dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_styled"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="15dp"
        android:theme="@style/AppTheme.AppBarOverlay.Styled">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/action_bar_background"/>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

工具栏样式的XML代码:

<style name="AppTheme.AppBarOverlay.Styled" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:background">@drawable/action_bar_background</item>
        <item name="android:titleTextStyle">@style/ToolBarTitleStyle</item>
    </style>
<style name="ToolBarTitleStyle" parent="Base.TextAppearance.AppCompat">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16dp</item>
    </style>

这是与工具栏相关的 MainActivity 代码:

toolbar = (Toolbar) findViewById(R.id.toolbar_styled);
toolbar.setTitle(R.string.app_name);

setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);

这是布局编辑器预览和虚拟设备的图像:

布局编辑器:https://i.gyazo.com/2b01f5ef9c87ecb35a605aa150aa6ad5.png

虚拟设备:https://i.gyazo.com/5bbd9bf5a2df1727278ba9e78efd622b.png

【问题讨论】:

  • 你为什么将它包装在 AppBarLayout 中?
  • 您解决了问题?

标签: android android-actionbar toolbar


【解决方案1】:

你需要像这样嵌套它:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cor_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="256dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_styled"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:elevation="15dp"
            android:theme="@style/AppTheme.AppBarOverlay.Styled">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/action_bar_background"/>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

然后像这样设置它们:

   toolbar= (Toolbar) findViewById(R.id.toolbar);
    collapse=(CollapsingToolbarLayout) findViewById(R.id.collapseToolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    collapse.setTitle("Title");
    collapse.setCollapsedTitleTextColor(Color.parseColor("#FFFFFF"));
    collapse.setExpandedTitleColor(Color.parseColor("#FFFFFF"));
    collapse.setStatusBarScrimColor(Color.parseColor("#FFFFFF"));

【讨论】:

    【解决方案2】:

    1) 在您的 res 文件夹中创建一个 tool_bar.xml 文件并粘贴以下代码。 2) 如有需要,请包含此 tool_bar.xml 文件。 3)在.java文件中实例化tool_bar对象并做进一步处理。

    <android.support.v7.widget.Toolbar
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="4dp"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/action_bar_background"/>
    </android.support.v7.widget.Toolbar>
    
    
    <include id="@+id/tool_bar"
             layout="@layout/tool_bar"/>
    
    Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多