【问题标题】:Android Toolbar Covering ContentAndroid 工具栏覆盖内容
【发布时间】:2015-02-28 18:23:45
【问题描述】:

我正在使用新工具栏并将其设置为 supportActionBar。我的问题是它覆盖了我的内容。

这是我用来设置工具栏的代码。

 public void setupNavBar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);

        mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
    }
}

风格:

    <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="ToolBarStyle" parent="">
    <item name="android:elevation">5dp</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

和布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    style="@style/ToolBarStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/material_blue_grey_800"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    android:id="@+id/toolbar_actionbar"

    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#478"
    android:text="@string/hello_world" />

我认为通过调用 setSupportActionbar 系统会负责将内容定位在工具栏视图下方,但我所看到的并非如此。

使用工具栏的正确方法是什么。我有一些活动,我希望工具栏对它背后的内容透明,而在其他一些活动中,我希望它对下面的内容不透明。

谢谢

【问题讨论】:

    标签: android android-actionbar android-styles android-toolbar


    【解决方案1】:

    在文本视图中使用下面

        <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#478"
    android:text="@string/hello_world"
    android:layout_below="@+id/toolbar_actionbar"
    />
    

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题。我通过在 context_main.xml 文件中添加这两行来修复它:

      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.yourappname.MainActivity"
      

      像这样:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout 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:background="@color/blue"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:context="com.yourappname.MainActivity"
      tools:showIn="@layout/app_bar_main">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-16
        • 1970-01-01
        • 2015-08-27
        • 2015-11-29
        • 1970-01-01
        • 2017-05-10
        • 1970-01-01
        相关资源
        最近更新 更多