Подтвердить что ты не робот

Помещение содержимого ниже AppBarLayout в CoordinatorLayout

Я очень новичок в Android, и я намеревался опубликовать это в Android Developers - Google Groups, но они, похоже, говорят, что нужны новички для публикации в Stack Overflow. Так что я здесь.

Я загрузил самую последнюю версию Android Studio 1.4.1 вчера, и я выполнил инструкции по Создание вашего первого приложения. Я сделал все до Запуск другого мероприятия. Кажется, что эти инструкции немного старые, то есть для предыдущей версии SDK, потому что они не ссылаются на CoordinatorLayout и AppBarLayout, хотя они появляются в коде, если вы выполните шаги. Очевидно, что я сделал небольшие изменения в коде, чтобы заставить это приложение работать, но не полностью.

Моя проблема: Если вы посмотрите на изображения внизу Запуск другого мероприятия, вы увидите, что оба из них имеют название My First App. В моих модификациях кода я не мог получить этот заголовок как на изображениях, так и на экранах. (Я должен упомянуть, что я хочу использовать более новую версию 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>

Как упоминалось в нижней части Построение простого пользовательского интерфейса 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 идет за Toolbar от AppBarLayout, а не ниже. Любые идеи по устранению этой проблемы?

4b9b3361

Ответ 1

Макеты в CoordinatorLayout должны определить layout_behavior. Измените свой контент на:

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