【发布时间】:2015-11-11 21:56:34
【问题描述】:
我对 Android 非常陌生,我打算将其发布到 Android Developers - Google Groups,但他们似乎说新手需要发布到 Stack Overflow。所以我来了。
我昨天下载了最新版本的 Android Studio 1.4.1,并按照Building Your First App 上的说明进行操作。我做了所有事情到Starting Another Activity。这些说明似乎有点旧,即对于 SDK 的早期版本,因为它们没有引用 CoordinatorLayout 和 AppBarLayout,尽管如果您按照步骤操作它们会出现在代码中。显然,我确实对代码进行了细微的更改以使该应用程序正常工作,但并非完全如此。
我的问题:如果您查看Starting Another Activity 底部的图像,您会看到它们都有标题我的第一个应用程序。在我对代码的修改中,我无法在两个图像/屏幕上获得这个标题。 (我应该提到我想使用更新版本的AppBarLayout 和CoordinatorLayout)
让我们关注第一个屏幕,activity_my.xml 是
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".MyActivity">
<include layout="@layout/content_my" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
正如Building a Simple User Interface 底部提到的,content_my.xml 看起来像:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
无论如何,我可以将AppBarLayout 添加到activity_my.xml。我试过类似的东西:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".MyActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_my" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
问题在于content_my.xml 中的内容位于AppBarLayout 的Toolbar 后面,而不是在它下面。任何想法如何解决这个问题?
【问题讨论】:
-
这似乎是 2 个单独的问题,至于您的第一个问题,要在工具栏中获取应用程序标题,请务必调用
setSupportActionBar(toolbar),其中toolbar是对您的工具栏视图的引用。此外,之所以没有引用CoordinatorLayout和AppBarLayout,是因为在以前的Android Studio 版本中,“空白活动”创建了一个完全空白的活动。它现在使用这两个元素创建一个简单的布局。新选项称为“空活动”。