将内容放在AppBarLayout下面的CoordinatorLayout中 [英] Putting content below AppBarLayout in a CoordinatorLayout

查看:78
本文介绍了将内容放在AppBarLayout下面的CoordinatorLayout中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触Android,因此打算将其发布到 Android开发人员-Google网上论坛,但是他们似乎说,新手需要发布到Stack Overflow.所以我在这里.

I'm very new to Android and I intended to post this to the Android Developers - Google Groups but they seem to say that newbies need to post to Stack Overflow. So I'm here.

昨天我下载了最新版本的 Android Studio 1.4.1 ,并且按照开始另一项活动.这些说明似乎有点过时,即对于SDK的早期版本,因为如果按照以下步骤操作,尽管它们出现在代码中,但它们并未引用CoordinatorLayoutAppBarLayout.显然,我确实在代码中做了一些细微的更改,以使该应用程序正常运行,但并非完全如此.

I downloaded the most recent version of Android Studio 1.4.1 yesterday, and I followed the instructions on Building Your First App. I did everything up to Starting Another Activity. It seems these instructions are a bit old i.e. for a previous version of the SDK, because they do not reference CoordinatorLayout and AppBarLayout though they appear in the code if you follow the steps. Obviously, I did make minor changes in the code to get this app to work, but not completely.

我的问题:如果您查看开始另一个活动,您将看到两个标题均为我的第一个应用.在修改代码时,我在两个图像/屏幕上都无法获得此标题. (我应该提到我想使用AppBarLayoutCoordinatorLayout的最新版本)

My Problem: If you look at the images at the bottom of Starting Another Activity you will see that both of them have the title My First App. In my modifications of the code, I could not get this title on both the images/screens. (I should mention that I want to use the more recent version of AppBarLayout and CoordinatorLayout)

我们将焦点放在第一个屏幕上,activity_my.xml

Let's focus on the first screen, the activity_my.xml is

<?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>

构建简单用户界面 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.我已经尝试过类似的东西:

Is there anyway, I can add the AppBarLayout to the activity_my.xml. I have tried something like:

<?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中的内容位于AppBarLayoutToolbar之后而不是其下方.有什么想法可以解决此问题吗?

The problem with this is that the content in content_my.xml goes behind the Toolbar of AppBarLayout rather than below it. Any ideas how to fix this issue?

推荐答案

CoordinatorLayout中的布局需要定义layout_behavior.将您的内容更改为此:

Layouts in a CoordinatorLayout need to define a layout_behavior. Change your content to this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>

<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下面的CoordinatorLayout中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆