汉堡菜单未显示

时间:2018-11-03 17:12:10

标签: java android android-studio android-layout

我正在尝试在MainActivity上集成一个汉堡菜单按钮,但确实如此 没有出现在活动中。我以the documentation的身份关注,但在显示汉堡菜单方面仍然存在一些问题。

我想念什么吗?

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

MainActivity页面

public class MainActivity extends AppCompatActivity
{

    DataBaseHelper myDb;

    TextView ChampText;

    EditText editText;

    Button ViewAll;

    EditText Time_TextView;

    Toolbar toolbar;

    private DrawerLayout mDrawerLayout;

    private ActionBarDrawerToggle mToggle;


    public void buttonOnClick3 (View v){
        ImageButton personaButton = findViewById(R.id.imageButton2);
        personaButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(MainActivity.this , Before_SecondScreen.class);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DataBaseHelper(this);
        Button Admin_Button = findViewById(R.id.Admin_Button);
        Button button_KANJI = findViewById(R.id.button_KANJI);
        Button button_HIRAGANA = findViewById(R.id.button_HIRAGANA);
        Button button_KATAKANA = findViewById(R.id.button_KATAKANA);
        Button LogInButton = findViewById(R.id.LogIn);
        Button SignIn_button = findViewById(R.id.SignIn_button);
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        TextView Time_TextView = findViewById(R.id.Time_TextView);
        Time_TextView.setText(currentDateTimeString);
        User user = (User)getIntent().getSerializableExtra("user");
        if(user != null ){
        TextView User_TextView = findViewById(R.id.User_TextView);
        User_TextView.setText("Welcome back " + " " + user.GetPseudo());
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        actionBar.setHomeButtonEnabled(true);
        User_TextView.setText(getString(R.string.nav_header_title));
        }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:activity_main.xml

<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:context="x.x.MainActivity"
    tools:openDrawer="start">

和MainActvity.java中的

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

以及最后的res / layout / values / string.xml

<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>