After creating a toolbar to replace an action bar, the toolbar is not producing the expected results. The toolbar itself appears with the specified colour as desired, but for some reason the title, subtitle and back button do not appear at all. The activity's theme has even been set to Theme.AppCompat.NoActionBar
.
layout/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();
}
}
Try this,
XML File:
<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 Class:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.