简体   繁体   中英

Android: Toolbar in multiple activities

I am trying to create a toolbar in my activities. However, it doesn't work, there is nothing in the action bar. I tried to make my activities extend from BaseActivity, which has toolbar configuration.

I think that there is problem with

onCreateOptionsMenu(Menu menu)

BaseActivity.java

public abstract class BaseActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResource());
    configureToolbar();
}
protected abstract int getLayoutResource();

private void configureToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

}
// Menu icons are inflated just as they were with actionbar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}
//Handle an option being selected on the menu
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.selPg:
            SelectionPage();
            return true;
        case R.id.choosePg:
            ChoosingPage();
            return  true;
        case R.id.addPg:
            AddingPage();
            return  true;
        default:
            return super.onOptionsItemSelected(item);
    }
               //....//
}

ChildActivity.java

public class form_activity extends BaseActivity {
  @Override
  public int getLayoutResource() {
  return R.layout.add_form;
  }


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

Child.xml

<!-- Load the toolbar here -->
    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item   android:id="@+id/selPg"
        android:icon="@android:drawable/ic_menu_help"
        android:title="Selecton Page"/>
<item   android:id="@+id/choosePg"
    android:icon="@android:drawable/ic_menu_directions"
    android:title="Choosing Page"/>
<item   android:id="@+id/addPg"
    android:icon="@android:drawable/ic_menu_add"
    android:title="Adding Page"/>
</menu>

tool_bar.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:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="@android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>

styles.xml

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.NoActionBar">

manifest

 android:theme="@style/AppCompatTheme">

In your manifest do like this:

    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar" />

Override in styles xml file

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

In your Layout XML add the toolbar:

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

Finally in your Main Activity which extends AppCompatActivity inflate the toolbar:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setTitle("My Activity");
    setSupportActionBar(toolbar);

您将setContentView设置了两次(在BaseActivity和ChildActivity中)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM