为什么在CoordinatorLayout中不起作用布局约束

时间:2019-02-01 15:31:55

标签: android xml android-layout layout

在我的android应用程序中,我使用CoordinatorLayout和AppBarLayout,CollapsingToolbarLayout 但是某些功能不起作用。 在CardView中,我无法使用app:layout_constraint“开始,顶部,结束和底部”移动TextViews

With click we can see my UI

       <android.support.design.widget.CoordinatorLayout
               xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context=".activities.DetailActivity"
                    android:fitsSystemWindows="true">
                <android.support.design.widget.AppBarLayout
                        android:id="@+id/main.appbar"
                        android:layout_width="match_parent"
                        android:layout_height="150dp"
                        android:fitsSystemWindows="true">

                    <android.support.design.widget.CollapsingToolbarLayout
                            android:id="@+id/main_collapsing"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            app:layout_scrollFlags="scroll|exitUntilCollapsed"
                            android:fitsSystemWindows="true"
                            app:contentScrim="?attr/colorPrimary"
                            app:expandedTitleMarginStart="48dp"
                            app:expandedTitleMarginEnd="64dp"
                            android:background="@drawable/Ar-Rahman">
                        <android.support.v7.widget.Toolbar
                                android:id="@+id/main_toolbar"
                                android:layout_width="match_parent"
                                android:layout_height="attr/actionBarSize"
                                android:minHeight="?attr/actionBarSize"
                                app:layout_collapseMode="pin"/>
    </android.support.design.widget.CollapsingToolbarLayout>
                </android.support.design.widget.AppBarLayout>
                <android.support.design.widget.FloatingActionButton
                        android:id="@+id/fab"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="16dp"
                        app:layout_anchor="@id/main.appbar"
                        app:layout_anchorGravity="bottom|end"/>

                <android.support.v4.widget.NestedScrollView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior">

                    <android.support.v7.widget.CardView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <TextView android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:id="@+id/arabic_name"
                                  android:text="Arabic_name"
app:layout_constraintStart_toStartOf="parent"
                         app:layout_constraintBottom_toBottomOf="parent"
                             app:layout_constraintEnd_toTopof="translate"/>
                        <TextView android:layout_width="wrap_content"
                                  android:text="@string/example_transate"
                                  android:id="@+id/translate"
        app:layout_constraintStart_toStartOf="parent"
                                  android:layout_height="wrap_content"/>
                    </android.support.v7.widget.CardView>
                </android.support.v4.widget.NestedScrollView>
            </android.support.design.widget.CoordinatorLayout>

我要在CardView 1st textView中的第二个TextView下面

1 个答案:

答案 0 :(得分:0)

要在CardView中设置视图约束,您必须在CardView中使用ConstraintLayout,并且必须将所有视图放入该ConstraintLayout中。然后将仅设置约束。

您不能设置约束来引导CardView的子级。约束属性仅适用于ConstraintLayout

的直接子级

尝试以下代码:

<?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=".activities.DetailActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:fitsSystemWindows="true">

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_anchor="@id/main.appbar"
        app:layout_anchorGravity="bottom|end" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <TextView
                    android:id="@+id/arabic_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Arabic_name"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/translate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/example_transate"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/arabic_name" />
            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.CardView>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>