首页 > 解决方案 > 使用 listView 将工具栏添加到活动

问题描述

我正在尝试向我的活动添加一个工具栏,它只有一个元素列表作为内容。这是我的代码,但它不起作用,因为它说我正在使用多个根。这怎么可能?

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp" >
</TextView>

<android.support.v7.widget.Toolbar
  android:id="@+id/my_toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  style="@style/HeaderBar"
  app:theme="@style/ActionBarThemeOverlay"
  app:popupTheme="@style/ActionBarPopupThemeOverlay"
  android:elevation="4dp"/>

标签: javaandroidlayoutroot

解决方案


是的……安卓是对的。

只能有一个根布局。您可以添加相对布局或线性布局作为根布局。根布局将包装您的文本视图和工具栏。

谢谢


推荐阅读