Android:多项活动中的工具栏

时间:2017-10-01 07:30:47

标签: java android android-toolbar

我正在尝试在我的活动中创建一个工具栏。但是,它不起作用,动作栏中没有任何内容。我试图让我的活动从BaseActivity扩展,它有工具栏配置。

我认为

存在问题
  

onCreateOptionsMenu(菜单菜单)

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">

清单

 android:theme="@style/AppCompatTheme">

2 个答案:

答案 0 :(得分:0)

在你的清单中这样做:

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

覆盖样式xml文件

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

在Layout XML中添加工具栏:

<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">

最后,在扩展AppCompatActivity的主要活动中,扩展工具栏:

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

答案 1 :(得分:0)

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