2017-02-13 50 views
0

创建工具栏以替换操作栏后,工具栏不会生成预期结果。工具栏本身会根据需要显示指定的颜色,但由于某些原因,标题,副标题和后退按钮完全不显示。活动的主题甚至被设置为Theme.AppCompat.NoActionBar尽管设置属性,Android工具栏标题,字幕和后退按钮不会出现

布局/ toolbar_turquoise.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar_turquoise" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="@color/turquoise" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> 

TurquoiseActivity.java

public class TurquoiseActivity extends AppCompatActivity { 

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

     ActionBar actionBar = getSupportActionBar(); 
     if(actionBar != null) { 
      Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_turquoise); 
      setSupportActionBar(myToolbar); 

      myToolbar.setTitle("Hello World"); 
      myToolbar.setTitleTextColor(Color.parseColor("#000099")); 
      myToolbar.setSubtitle("Lorem ipsum dolor sit amet"); 
      myToolbar.setSubtitleTextColor(Color.parseColor("#000099")); 

      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

      final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material); 
      upArrow.setColorFilter(Color.parseColor("#000099"), PorterDuff.Mode.SRC_ATOP); 
      getSupportActionBar().setHomeAsUpIndicator(upArrow); 
     } 

     FragmentTurquoise newFragment = new FragmentTurquoise(); 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.master_container, newFragment); 
     transaction.commit(); 
    } 
} 

回答

1

试试这个,

XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".MainActivity"> 

     <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/turquoise" 
      android:minHeight="100dp"> 

     </android.support.v7.widget.Toolbar> 

    </RelativeLayout> 

Java类:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    if(toolbar != null) { 
     setSupportActionBar(toolbar); 

     toolbar.setTitle("Hello World"); 
     toolbar.setTitleTextColor(Color.parseColor("#000099")); 
     toolbar.setSubtitle("Lorem ipsum dolor sit amet"); 
     toolbar.setSubtitleTextColor(Color.parseColor("#000099")); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material); 
     upArrow.setColorFilter(Color.parseColor("#000099"), PorterDuff.Mode.SRC_ATOP); 
     getSupportActionBar().setHomeAsUpIndicator(upArrow); 
    }