gpt4 book ai didi

java - Android 抽屉布局未显示

转载 作者:行者123 更新时间:2023-12-01 10:50:22 28 4
gpt4 key购买 nike

我是 Android 开发新手。我正在尝试实现抽屉布局,构建中没有错误,但抽屉汉堡包图标不显示。这是我的 xml 布局

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"
/>

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/drawer"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ListView android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
/>

</android.support.v4.widget.DrawerLayout>

</LinearLayout>

MainActivity.java

package com.example.arslan.tutsplus02;

import android.content.res.Configuration;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_opened,R.string.drawer_closed)
{
@Override
public void onDrawerOpened(View drawerView) {
if (getSupportActionBar()!= null ) {
getSupportActionBar().setTitle(R.string.drawer_opened);
}
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (getSupportActionBar()!= null ) {
getSupportActionBar().setTitle(R.string.drawer_closed);
}
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}

@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;
}

@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();

if (mActionBarDrawerToggle.onOptionsItemSelected(item))
{
return true;
}

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

谢谢

最佳答案

尝试将 Toolbar 添加到 ActionBarDrawerToggle。

mActionBarDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.drawer_opened,R.string.drawer_closed)
{
@Override
public void onDrawerOpened(View drawerView) {
if (getSupportActionBar()!= null ) {
getSupportActionBar().setTitle(R.string.drawer_opened);
}
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (getSupportActionBar()!= null ) {
getSupportActionBar().setTitle(R.string.drawer_closed);
}
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

关于java - Android 抽屉布局未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33955890/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com