the back button and the title is not shown when setup toolbar in android

时间:2018-03-25 18:54:07

标签: java android toolbar

I'm tying to setup the toolbar in the activity, but the when i set a title and the default back button. it doesn't show anything

 public class LaptopDetail extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_laptop_detail);


    setupToolbar();
}


void setupToolbar() {

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Intent data = getIntent();
    getSupportActionBar().setTitle(data.getStringExtra("laptop_name"));
    getSupportActionBar().setDisplayShowHomeEnabled(true);

}


public boolean onOptionsItemSelected(MenuItem item){
    Intent myIntent = new Intent(getApplicationContext(), BrandOffers.class);
    startActivityForResult(myIntent, 0);
    return true;

}
}

and here is my xml

 <RelativeLayout 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"
tools:context="com.example.ahmed.myapplication3.LaptopDetail">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#273243"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

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

</RelativeLayout>
</RelativeLayout>

is there anything missing in my code, by the way, I'm using the same code in different activity and it is working thanks in advance

1 个答案:

答案 0 :(得分:0)

You haven't included the content of your theme style, but I think that's where the problem might be. Check out this answer, I hope it helps.