对如何在我的第二个活动上显示操作栏感到困惑。希望我没有提供太多。基本上澄清一下,动作栏在我的MainActivity(标题显示和设置按钮)上工作得很好,但我只能在其他活动上看到动作栏(没有标题或按钮)我还看了一下menu_main.xml并注意到了上下文设置为我的MainActivity,所以我创建了menu_attractions.xml并将上下文更改为正确的活动,但仍然没有显示标题或设置按钮。任何帮助将不胜感激!
我的MainActivity没什么好看的(这是我认为标题/按钮会发生魔力的地方吗?)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<include layout="@layout/content_main" />
<include layout="@layout/app_bar"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
对于MainActivity类,我搜索下面的工具栏...并膨胀菜单
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
attractionButton = (Button) findViewById(R.id.activityButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
这是我的操作栏
<?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/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
public class AttractionsActivity extends AppCompatActivity {
private Button scheduleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.d_activities);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
scheduleButton = (Button) findViewById(R.id.activies_Schedule);
scheduleButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view){
Intent scheduleIntent = new Intent(AttractionsActivity.this, ScheduleActivity.class);
startActivity(scheduleIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_attractions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/img_252413"
tools:context="AttractionsActivity"
tools:showIn="@layout/d_activities">
<include layout="@layout/app_bar"
android:id="@+id/include" />
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/include">
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Schedule"
android:id="@+id/activies_Schedule" />
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Maps"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Performers"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Sponsors"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Statics"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Exibitors"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Food"/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
您似乎已更改了第二个活动的主题。请尝试以下
第1步:在 Styles.xml
中添加此内容<style name="AppTheme.AttractionsActivity" parent="Theme.AppCompat.Light.DarkActionBar">
第2步:在Manifest.Xml中添加
<activity
android:name=".AttractionsActivity"
android:theme="@style/AppTheme.AttractionsActivity"
/>