它是我的mainactivity文件。
public class Mainactivity extends AppCompatactivity {
DrawerLayout drawerLayout;
private actionbardrawertoggle actionbardrawertoggle;
private Toolbar toolbar;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar) findViewById(R.id.tool_bar_main);
setSupportactionBar(toolbar);
getSupportactionBar().setTitle("Bhandan");
drawerLayout = findViewById(R.id.drawer);
actionbardrawertoggle = new actionbardrawertoggle(this,drawerLayout,R.string.Open,R.string.Close);
drawerLayout.addDrawerListener(actionbardrawertoggle);
actionbardrawertoggle.syncState();
getSupportactionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionbardrawertoggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
它是我的具有抽屉布局的xml文件。具有宽度和高度匹配的父对象。当我更改任何内容以通过异常包装它时。 enter image description here
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer"
>
<include
android:id="@+id/tool_bar_main"
layout="@layout/tool_bar"
>
</include>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation_view"
app:headerLayout="@layout/header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_drawer"
>
</com.google.android.material.navigation.NavigationView>
它是我的工具栏xml代码。
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.appcompat.widget.Toolbar>
将其标题放置在抽屉布局中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="260dp"
android:background="@drawable/header_image"
android:padding="30dp"
android:layout_marginTop="10dp"
>
<ImageView
android:layout_marginTop="40dp"
android:src="@drawable/circle_image"
android:layout_width="75dp"
android:layout_height="75dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ali Raees"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold"
android:layout_marginTop="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aliraees55@gmail.com"
android:textColor="@color/colorPrimaryDark"
/>
如果我设置工具栏的颜色会覆盖整个区域而不是工具栏背景,则会出现另一个问题。