2017-01-04 177 views
0

我想要在使用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()解决了我的问题。有关如何修复工具栏高度的任何想法?

Toolbar height too small

回答

1

你应该使用AppCompatActivity,不FragmentActivity

public ScrollingActivity extends AppCompatActivity { 

public ScrollingActivity extends FragmentActivity { 
+0

你能解释这个答案吗?由于OP的问题并不清楚(这就像一个线程中的两个问题),我认为你的答案不在正确的位置。这可能是一条评论... – Mohsen

+0

他正在使用'FragmentActivity'而不是'AppCompatActivity'与'CoordinatorLayout'结合使用。不,这不应该是一个评论,我已经运行他的代码,转载问题并修复它,正如我在我的回答中所述。如果你不明白问题,那并不意味着每个人都这样做。 – Divers

+0

哦,你说得对。没有想到/看到那个'片段活动'。此时你是对的!我只是提出你的答案。 – Mohsen

1

要解决此问题,您可能要更改的AppBarLayoutandroid:layout_heightwrap_content

所以:

<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://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

UPDATE:

说实话,我也没多想FragmentActivity 。不管怎么说,这是你的答案:

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

的支持库的最新版本,你应该让你的 Activity延长AppCompatActivityActionBarActivity一直 弃用。

但在您的情况,这可以帮助:

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar) 

如果这没有工作,那么,

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

+0

你试过了吗?不,它不能解决问题。 – Divers

+0

现在它应该工作:) – Mohsen

+0

你的更新答案如何反映作者的问题?作者使用'FragmentActivity',而不是'ActionBarActivity'。另外,如果他将尝试将'FragmentActivity'强制转换为'AppCompatActivity',它将会抛出异常。 – Divers