gpt4 book ai didi

java - 尝试将工具栏包含到使用 NavDrawer 模板的应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 07:55:42 24 4
gpt4 key购买 nike

我正在尝试将旧应用移至新的 Material 设计指南。我之前使用 Navigation Drawer Template 来创建 Main Activity。但是,我在尝试将工具栏实现到主要 Activity 时遇到了一些大问题。我不断收到 NullPointer 异常。

我正在创建一个这样的工具栏 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue" >
</android.support.v7.widget.Toolbar>

之后,我尝试使用 include 命令将它添加到我的主布局中:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.medinfo.main.MainActivity" >

<include layout="@layout/toolbar_database_edit" />

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

<fragment
android:id="@+id/navigation_drawer"
android:name="com.medinfo.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />

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

在我的 activity_main 中,我正在执行以下操作:NPE 在我调用 setSupportActionBar 或调用 Setup NavDrawer 时发生。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);
setSupportActionBar(toolbar);

mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();

// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}

我尝试将 DrawerLayout 包裹在线性布局中,所做的是在包含我的 fragment 的容器后面创建工具栏,当我开始 Activity 时,我看到工具栏在后台呈现,然后在容器和 fragment 被隐藏后隐藏加载。但是它确实删除了 NPE。我还尝试在布局中创建工具栏,而不是使用单独的工具栏 XML 文件。我仍然遇到相同的 NPE 错误。

虽然我在询问新的 Material Design Stuff,但我也想知道是否有办法让我的 EditText 小部件和 Spinner 小部件使用旧的 Holo 主题/样式。

EditText Holo

EditText Material

Spinner Holo

Spinner Material

我真的希望有一种简单的方法可以恢复到这些样式。

谢谢大家的帮助

最佳答案

1) 首先,您的布局不正确。 DrawerLayout 接受两个 child :布局容器和抽屉布局本身。

简化,

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include layout="@layout/activity" />
<include layout="@layout/drawer" />

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

其次,您错误地findViewById您的工具栏。

Toolbar toolbar = (Toolbar) findViewById(R.layout.toolbar_database_edit);

此处您引用的是布局,但应引用 id。

鉴于上面的布局资源,我们有activity.xml:

<LinearLayout
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:background="@color/white"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/blue"
android:foreground="?android:attr/windowContentOverlay"
app:theme="@style/Toolbar"/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

. . . contents of activity layout

</FrameLayout>

</LinearLayout>

在代码中:

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

2) 要使用旧的 Holo 样式,您应该在 styles.xml 中使用 Holo 主题作为应用主题的父主题。但是用户会期望每个 Android 版本都有原生主题,所以这是一个不好的做法。

关于java - 尝试将工具栏包含到使用 NavDrawer 模板的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28028385/

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