我在编程方面非常陌生,尤其是在Android Studio中。因此,我正在为学期论文制作一个应用程序。我想通过导航抽屉进行导航,可以通过工具栏中的按钮将其打开。工具栏没有显示,但是当我在应用程序中单击按钮应位于的位置时,抽屉就会打开。
这是我的主要活动xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/colorBack">
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"
android:background="@color/colorBack">
<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" />
<ImageView
android:id="@+id/MainPic"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/pic_9974" />
<ImageView
android:id="@+id/SpacerTop"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/rectangle" />
<ImageView
android:id="@+id/SpacerBot"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="258dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/rectangle2" />
<TextView
android:id="@+id/welcomeHead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_head"
android:gravity="center"
android:textSize="20dp"
android:textColor="@color/colorTop"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@+id/SpacerBot" />
<TextView
android:id="@+id/welcomeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_text"
android:textColor="@color/colorTop"
android:textSize="18dp"
android:textStyle="bold"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@+id/welcomeHead" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/header_layout"/>
</android.support.v4.widget.DrawerLayout>
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appbm">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/TestTheme"
android:background="@color/colorBack">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的样式:
<resources>
<style name="TestTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorTop</item>
<item name="colorPrimaryDark">@color/colorTopDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
这是我主要活动的一部分:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
不幸的是,在其他帖子中,我无能为力。
我希望有人能帮助我。
圆顶K。
答案 0 :(得分:0)
在您的 main activity xml
中使用它:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#ffffff"
app:popupTheme="@style/AppTheme.PopupOverlay">
// customize your toolbar
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_dashboard"
app:itemIconTint="@drawable/drawer_item"
app:itemTextColor="@drawable/drawer_item"
app:menu="@menu/activity_dashboard_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
在 style.xml
中:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
// for NoActionBar
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
// for AppBar
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
// for Toolbar
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
和 java类中仅将其用于工具栏:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
在清单文件中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appbm">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" // change here
android:background="@color/colorBack">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 1 :(得分:0)
将此添加到您的MainActivity
setSupportActionBar(toolbar);
toolbar.bringToFront();