1

我爲我的應用程序使用應用程序主題,其中操作欄標誌對於該主題爲真。我的所有活動都顯示了相同的操作欄。如何使用CollapsingToolbarLayout而不是使用應用程序主題操作欄進行特定的活動?

如何使用CollapsingToolbarLayout進行任何特定活動而不是使用主應用主題操作欄?

我已經添加下面的代碼在我的活動類:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     CollapsingToolbarLayout collapsingToolbar = 
       (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
     collapsingToolbar.setTitle(cheeseName); 

這是我的佈局:

<android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginStart="48dp" 
      app:expandedTitleMarginEnd="64dp"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:layout_collapseMode="pin" /> 

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

但它不工作,我的活動處處顯示着應用主題行動吧。

回答

1

試試這個,

xml文件

<android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       android:src="@drawable/your_image" 
       app:layout_collapseMode="parallax" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:popupTheme="@style/ThemeOverlay.AppCompat.Dark" 
       android:title="Collapsing Toolbar" 
       app:layout_collapseMode="pin" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 
+0

我已經試過這種方式沒有爲我工作。 – 0xalihn

+0

你會得到什麼? –

+0

您尚未使用任何圖像進行摺疊。使用圖像它將工作 –

0

@ 0xAliHn,

它,因爲你沒有應用任何主題,以CollapsingToolbarLayout

剛剛創建要的是定製的主題和應用它到你的AppBarLayout

其他明智它會繼承您已在Androidmenifest.xml

+0

我在我的AppBarlayout中使用了這個'android:theme =「@ style/ThemeOverlay.AppCompat.Dark.ActionBar」'' – 0xalihn

0

內部使用如果使用CollapsingToolbarLayout同一個主題,你需要設置Toolbar裏面的操作欄。您可以解決這種情況,隱藏您用於所有活動的操作欄(在本例中爲主工具欄)。之後,使用上面

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

對不起你的代碼,如果我不明白你想要什麼正確的事情。

0

你應該設置在mainifest新的主題這樣

<activity android:name=".your activity" android:theme="xxxx"></activity> 
0

試試這個:在你的XML佈局...

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

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

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/toolbar_layout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fitsSystemWindows="true" 
       app:contentScrim="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"> 

       <ImageView 
        android:id="@+id/header" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:adjustViewBounds="true" 
        android:src="@drawable/google_thumb" 
        app:layout_collapseMode="parallax" 
        app:layout_collapseParallaxMultiplier="0.7" /> 

       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        app:layout_collapseMode="pin" 
        app:layout_scrollFlags="scroll|exitUntilCollapsed" 
        app:popupTheme="@style/AppTheme.PopupOverlay" /> 

      </android.support.design.widget.CollapsingToolbarLayout> 
     </android.support.design.widget.AppBarLayout> 

     //this is the layout that you want under collapsing toolbar 
     <include layout="@layout/content_scrolling" /> 

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

在你Activity

public class Toolbar_activity extends AppCompatActivity { 

    CollapsingToolbarLayout collapsingToolbar; 
    Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.your_layout); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout); 
     collapsingToolbar.setTitle("TITLE"); 
    } 
} 

然後在您的清單:

<activity 
     android:name=".Toolbar_activity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@style/AppTheme.NoActionBar"></activity> 
相關問題