CollapsingToolbar和constraintLayout

时间:2018-06-06 12:34:46

标签: android layout

目前停留在布局上。 我想要在collaspingToolbar下显示带有编辑文本的布局。我拥有的是......好吧,不是那样的:D

尝试了许多事情,但我一直在努力。 (关于android开发的第一个真正的练习,不太习惯那些布局的东西)

任何帮助都会很棒。

    <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=".Activities.AddCustomerActivity"
    tools:ignore="MergeRootFrame">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/detail_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <EditText
            android:id="@+id/add_customer_lastname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Last name"
            app:layout_constraintTop_toBottomOf="@id/toolbar_layout"
            tools:ignore="NotSibling" />

        <EditText
            android:id="@+id/add_customer_firstname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="First name"
            app:layout_constraintTop_toBottomOf="@id/add_customer_lastname" />

        <EditText
            android:id="@+id/add_customer_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Address"
            app:layout_constraintTop_toBottomOf="@id/add_customer_firstname" />

        <EditText
            android:id="@+id/add_customer_postcode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Post code"
            app:layout_constraintTop_toBottomOf="@id/add_customer_address" />

        <EditText
            android:id="@+id/add_customer_city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="City"
            app:layout_constraintTop_toBottomOf="@id/add_customer_postcode" />

        <EditText
            android:id="@+id/add_customer_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Phone number"
            app:layout_constraintTop_toBottomOf="@id/add_customer_city" />


        <EditText
            android:id="@+id/add_customer_mail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="E-mail"
            app:layout_constraintTop_toBottomOf="@id/add_customer_phone" />

        <EditText
            android:id="@+id/add_customer_license"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="License number"
            app:layout_constraintTop_toBottomOf="@id/add_customer_mail" />

    </android.support.constraint.ConstraintLayout>


</android.support.design.widget.CoordinatorLayout>

以下是我目前的结果:

enter image description here

2 个答案:

答案 0 :(得分:2)

应用栏布局

 <android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

在NestedScrollView

中添加约束布局
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <EditText
        android:id="@+id/add_customer_lastname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="Last name"
        app:layout_constraintTop_toBottomOf="@id/toolbar_layout"
        tools:ignore="NotSibling" />

    <EditText
        android:id="@+id/add_customer_firstname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="First name"
        app:layout_constraintTop_toBottomOf="@id/add_customer_lastname" />

    <EditText
        android:id="@+id/add_customer_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="Address"
        app:layout_constraintTop_toBottomOf="@id/add_customer_firstname" />

    <EditText
        android:id="@+id/add_customer_postcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="Post code"
        app:layout_constraintTop_toBottomOf="@id/add_customer_address" />

    <EditText
        android:id="@+id/add_customer_city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="City"
        app:layout_constraintTop_toBottomOf="@id/add_customer_postcode" />

    <EditText
        android:id="@+id/add_customer_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="Phone number"
        app:layout_constraintTop_toBottomOf="@id/add_customer_city" />


    <EditText
        android:id="@+id/add_customer_mail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="E-mail"
        app:layout_constraintTop_toBottomOf="@id/add_customer_phone" />

    <EditText
        android:id="@+id/add_customer_license"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8sp"
        android:layout_marginStart="8sp"
        android:hint="License number"
        app:layout_constraintTop_toBottomOf="@id/add_customer_mail" />

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:2)

我创建了两个布局

activity_main.xml中

<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=".activities.ActivityName"
tools:ignore="MergeRootFrame">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

和content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".activities.ActivityName"
        tools:showIn="@layout/activity_main">

        <EditText
            android:id="@+id/add_customer_lastname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Last name"
            app:layout_constraintTop_toBottomOf="@id/toolbar_layout"
            tools:ignore="NotSibling" />

        <EditText
            android:id="@+id/add_customer_firstname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="First name"
            app:layout_constraintTop_toBottomOf="@id/add_customer_lastname" />

        <EditText
            android:id="@+id/add_customer_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Address"
            app:layout_constraintTop_toBottomOf="@id/add_customer_firstname" />

        <EditText
            android:id="@+id/add_customer_postcode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Post code"
            app:layout_constraintTop_toBottomOf="@id/add_customer_address" />

        <EditText
            android:id="@+id/add_customer_city"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="City"
            app:layout_constraintTop_toBottomOf="@id/add_customer_postcode" />

        <EditText
            android:id="@+id/add_customer_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="Phone number"
            app:layout_constraintTop_toBottomOf="@id/add_customer_city" />

        <EditText
            android:id="@+id/add_customer_mail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="E-mail"
            app:layout_constraintTop_toBottomOf="@id/add_customer_phone" />

        <EditText
            android:id="@+id/add_customer_license"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8sp"
            android:layout_marginStart="8sp"
            android:hint="License number"
            app:layout_constraintTop_toBottomOf="@id/add_customer_mail" />

    </android.support.constraint.ConstraintLayout>