布局 XML:
<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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="10dp" />
</android.support.v4.widget.DrawerLayout>
我想在 Activity 顶部添加一个工具栏
(类似于 ActionBar),如下所示:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
无论我在布局中的哪个位置包含此 View (基本上在所有地方都尝试过),我总是会收到 InflateException
。
appcompat-v7
库项目(据说)由 Eclipse 处理,作为库项目引用,使用最新的 (22.1.1) 版本。该 Activity 扩展了 AppCompatActivity
(也尝试使用 Activity
)。
不知道出了什么问题,我很感激任何建议。 如果您需要更多详细信息,请告知。
最佳答案
创建您的主要 xml,例如
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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">
<RelativeLayout
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="#00ffee"
>
</android.support.v7.widget.Toolbar>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"/>
</RelativeLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80000000"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="10dp"/>
</android.support.v4.widget.DrawerLayout>
将其添加到您的 styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/color_accent</item>
<item name="android:textColorPrimary">@color/color_primary_text</item>
</style>
关于java - 如何将工具栏添加到此布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30186378/