安卓操作栏不会显示标题或设置



对如何在我的第二个活动中显示操作栏有点困惑。希望我没有提供太多。基本上澄清一下,操作栏在我的 MainActivity 上完美运行(标题显示和设置按钮(,但我只能在其他活动(没有标题或按钮(上看到操作栏 我还看了一下menu_main.xml,注意到上下文设置为我的 MainActivity,所以我做了menu_attractions.xml并将上下文更改为正确的活动, 但这仍然没有显示标题或设置按钮。任何帮助将不胜感激!

对于我的主要活动来说没有什么花哨的(这就是我认为标题/按钮发生魔术的地方??

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

您似乎已更改第二个活动的主题。请尝试以下操作

第 1 步:将其添加到您的样式中.xml

<style name="AppTheme.AttractionsActivity" parent="Theme.AppCompat.Light.DarkActionBar">

步骤 2:在 Manifest.Xml 中添加此内容

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

最新更新