我想要在使用v4 compability库时在版本21+中显示Android状态栏下的单个工具栏(v7)。我阅读了很多帖子并尝试了很多不同的配置,但似乎无法手动设置填充(这是我想要防止的,因为SystemWindow的大小可能会在以后的Android版本中更改)或使用setSupportActionBar()
。Android FragmentActivity,AppBarLayout,工具栏和半透明状态栏
我从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款式的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()
解决了我的问题。有关如何修复工具栏高度的任何想法?
你能解释这个答案吗?由于OP的问题并不清楚(这就像一个线程中的两个问题),我认为你的答案不在正确的位置。这可能是一条评论... – Mohsen
他正在使用'FragmentActivity'而不是'AppCompatActivity'与'CoordinatorLayout'结合使用。不,这不应该是一个评论,我已经运行他的代码,转载问题并修复它,正如我在我的回答中所述。如果你不明白问题,那并不意味着每个人都这样做。 – Divers
哦,你说得对。没有想到/看到那个'片段活动'。此时你是对的!我只是提出你的答案。 – Mohsen