Android FragmentActivity,AppBarLayout,工具栏和半透明状态栏

时间:2017-01-04 15:05:39

标签: android android-toolbar android-appbarlayout

我正在尝试使用单个工具栏(v7)在使用v4兼容性库时在21+版本的Android状态栏下显示。我阅读了很多帖子并尝试了很多不同的配置,但似乎无法在不设置手动填充的情况下工作(这是我想要防止的,因为SystemWindow的大小可能会在以后的Android版本中发生变化)或使用setSupportActionBar()

我开始使用Android Studio中的默认滚动示例,并尝试用常规工具栏替换CollapsingToolbarLayout。

我的布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.wbusey0.toolbartest.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.AppBarOverlay" />
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_scrolling" />

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

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- 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" />

    <style name="AppTheme.PopupOverlay"  />

</resources>

v21 styles xml:

    <style name="AppTheme.NoActionBar" parent="@style/Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wbusey0.toolbartest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ScrollingActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我不能使用setSupportActionBar()因为我想稍后使用嵌套片段,因此喜欢扩展FragmentActivity。但是使用setSupportActionBar()解决了我的问题。关于如何修复工具栏高度的任何想法?

Toolbar height too small

2 个答案:

答案 0 :(得分:1)

您应该使用AppCompatActivity,而不是FragmentActivity

public ScrollingActivity extends AppCompatActivity {

不会

public ScrollingActivity extends FragmentActivity {

答案 1 :(得分:1)

要解决此问题,您可能需要将android:layout_height的{​​{1}}更改为AppBarLayout

<强>所以:

wrap_content

除了文档说同样的事情:

https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

<强>更新

老实说,我没有想到<android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay"> 。无论如何,这是你的答案:

https://stackoverflow.com/a/30371919/4409113

  

使用最新版本的支持库,你应该制作你的   FragmentActivity已延长Activity AppCompatActivity   弃用。

但在你的情况下,这可以提供帮助:

ActionBarActivity

如果这不起作用,那么;

正如@Divers所说,你需要将'Activity'扩展为'AppCompatActivity'..并且只需初始化'Toolbar'。