2015-06-17 46 views
-1

我已经尝试了每个堆栈溢出的答案,但没有任何工作。代码如下,请告诉我,如果我错过了什么:主页图标没有出现在工具栏上

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.design.widget.CollapsingToolbarLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.graphics.Palette; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 

import com.suleiman.material.R; 
import com.suleiman.material.adapter.SimpleRecyclerAdapter; 
import com.suleiman.material.model.VersionModel; 

import java.util.ArrayList; 
import java.util.List; 

public class AnimateToolbar extends AppCompatActivity { 
    CollapsingToolbarLayout collapsingToolbar; 
    RecyclerView recyclerView; 
    int mutedColor = R.attr.colorPrimary; 
    SimpleRecyclerAdapter simpleRecyclerAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_animate_toolbar); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.anim_toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); 


     collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
     collapsingToolbar.setTitle("Suleiman Ali Shakir"); 

     ImageView header = (ImageView) findViewById(R.id.header); 

     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.header); 

     Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { 
      @Override 
      public void onGenerated(Palette palette) { 

       mutedColor = palette.getMutedColor(R.attr.colorPrimary); 
       collapsingToolbar.setContentScrimColor(mutedColor); 
      } 
     }); 

     recyclerView = (RecyclerView) findViewById(R.id.scrollableview); 

     recyclerView.setHasFixedSize(true); 
     LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(linearLayoutManager); 

     List<String> listData = new ArrayList<String>(); 
     int ct = 0; 
     for (int i = 0; i < VersionModel.data.length * 2; i++) { 
      listData.add(VersionModel.data[ct]); 
      ct++; 
      if (ct == VersionModel.data.length) { 
       ct = 0; 
      } 
     } 

     if (simpleRecyclerAdapter == null) { 
      simpleRecyclerAdapter = new SimpleRecyclerAdapter(listData); 
      recyclerView.setAdapter(simpleRecyclerAdapter); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_animate_toolbar, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       finish(); 
       return true; 
      case R.id.action_settings: 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

我的XML代码:

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="192dp" 
     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:expandedTitleMarginEnd="64dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/header" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/header" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       app:layout_collapseMode="parallax" /> 

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

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

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/scrollableview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:clickable="true" 
     android:src="@drawable/ic_action_add" 
     app:layout_anchor="@+id/appbar" 
     app:layout_anchorGravity="bottom|right|end" /> 

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

android.R.id.home按钮并未显示工具栏。我已经使用过每一件东西,但之后它也无法正常工作。请帮忙。

回答

0

你这是怎么设置.. 使用它相应

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
setSupportActionBar(toolbar); 

toolbar.setNavigationIcon(R.drawable.ic_good); 
toolbar.setTitle("Title"); 
toolbar.setSubtitle("Sub"); 
toolbar.setLogo(R.drawable.ic_launcher); 
+0

感谢您的帮助,但它不工作!在这个代码中。 – Anupriya

+0

@Priya你可以在真实的设备上测试它吗?我有同样的问题,虽然它在模拟器上,但它发生在真实的设备上。 –

+0

我在真实设备上测试过,但后来也没有工作。现在我在工具栏中使用imageview并将其作为主图标。 – Anupriya

0

这也有帮助。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

ActionBar ab = getSupportActionBar(); 
ab.setHomeAsUpIndicator(R.drawable.ic_home); 
ab.setDisplayHomeAsUpEnabled(true); 
+0

对不起,但它不工作!在这个代码中。 – Anupriya

+0

@Priya这可能是怎么回事?这对我来说可以。 –

+0

雅我知道它的正确,但它不工作时实施工具栏视差滚动 – Anupriya

0

有时工具栏会放在后面。因此,您需要在Oncreate()方法中调用以下方法。这将解决问题

[toolbar object].bringToFront();