我正在开发一款应用,我希望每个活动都有一个工具栏。我将工具栏设置为主要活动,但是当我单击工具栏中的项目以将我发送到下一个活动时,工具栏不会显示。我已将工具栏包含在两个.xml文件中。当我单独运行第二个活动时,会显示工具栏。有什么帮助吗?
HomeActivty Java:
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.tToolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_home,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int res_id = item.getItemId();
if(res_id == R.id.action_profile)
{
Intent profileActivity = new Intent(HomeActivity.this,
ProfileActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
else if(res_id == R.id.action_home)
{
}
else if(res_id == R.id.action_msg)
{
Intent cnectActivity = new Intent(HomeActivity.this,
ConnectionsActivity.class);
startActivity(cnectActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
return true;
}
}
ProfileActivity java:
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
toolbar = (Toolbar) findViewById(R.id.tToolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_home,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int res_id = item.getItemId();
if(res_id == R.id.action_profile)
{
}
else if(res_id == R.id.action_home)
{
Intent profileActivity = new Intent(ProfileActivity.this,
HomeActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
else if(res_id == R.id.action_msg)
{
Intent cnectActivity = new Intent(ProfileActivity.this,
ConnectionsActivity.class);
startActivity(cnectActivity);
finish();
overridePendingTransition(R.anim.push_left_in,
R.anim.push_left_out);
}
return true;
}
}
toolbar xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
app:theme="@style/AppThemePenis"
android:elevation="4dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="P"/>
</android.support.v7.widget.Toolbar>
我正在使用Android Studio。