约束布局中的appbar / toolbar重叠视图

时间:2018-10-24 02:00:52

标签: android android-toolbar android-constraintlayout

我使用约束布局在垂直链中有六个按钮。在我的模拟器中,看起来不错。您可以忽略两侧的两个按钮。我添加了他们的观点。  enter image description here

当我将应用程序放置在实际的相应设备上时,我会得到截然不同的视图。工具栏与我的顶部按钮重叠,导航栏与我的底部按钮重叠。

enter image description here

我已经读到仿真器并不总是正确的。鉴于此,我该如何考虑约束布局中工具栏和导航栏的高度。下面是我的xml。这六个按钮在下面的xml中分别命名为x21到x26。我为它的长度表示歉意。有没有一种方法可以解决此问题,或者需要在我的代码中计算工具栏和导航栏的高度并更新顶部和底部按钮的边距?如果我需要在代码中执行此操作,该怎么做。也许还有另一种方法,我将不胜感激。 谢谢

<com.example.android.cop1803.LayoutCustom 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:id="@+id/mylayoutcustom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
tools:context="com.example.android.cop1803.MainActivity"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout_editor_absoluteY="73dp">


<com.example.android.cop1803.MyViewbutton
    android:id="@+id/MainCOPbtn"
    android:layout_width="@dimen/layoutwidthMaincopbutton"
    android:layout_height="@dimen/layoutheightMaincopbutton"
    android:layout_marginStart="0dp"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:background="@drawable/copbuttonmaineffect"
    android:text="@string/x11MainCOPtext"
    android:textAppearance="@style/MainButtonFontStyle"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.14"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"

    />
 <com.example.android.cop1803.MyViewbutton
    android:id="@+id/x21"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:background="@drawable/copbutton"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="@string/x21BreadingCrustCoatingsWraps"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toTopOf="@+id/x22"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="spread" />


<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x22"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:background="@drawable/copbutton"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:text="@string/x22CuttingandManipulation"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toTopOf="@+id/x23"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/x21" />

<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x23"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:background="@drawable/copbutton"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="@string/x23DryRubsandStuffings"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toTopOf="@id/x24"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/x22" />

<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x24"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:background="@drawable/copbutton"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="@string/x24HerbandSpicePastes"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toTopOf="@id/x25"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/x23" />

<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x25"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:background="@drawable/copbutton"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="@string/x25MarinadesandBrines"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toTopOf="@id/x26"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/x24" />

<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x26"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:background="@drawable/copbutton"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="@string/x26SpicesandSpiceCrusts"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toTopOf="@id/x27"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/x25" />

<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x27"
    android:layout_width="@dimen/layoutwidthcopbutton"
    android:layout_height="@dimen/layoutheightcopbutton"
    android:background="@drawable/copbutton"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="@string/x27Balance"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/x26" />

<com.example.android.cop1803.MyViewbutton
    android:id="@+id/x31"
    android:layout_width="@dimen/layoutwidthMedcopbutton"
    android:layout_height="@dimen/layoutheightMedcopbutton"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:background="@drawable/copbuttonsecond"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:text="@string/x31CookingTechniques"
    android:textAppearance="@style/ButtonFontStyle"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.64"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />



<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.android.cop1803.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme"
        >
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_main"
            />
    </android.support.design.widget.AppBarLayout>
    <include
        android:id="@+id/cartlist"
        layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>

3 个答案:

答案 0 :(得分:0)

我认为你的意思是行动吧。尝试将操作栏边距添加到XML的根元素。

 android:paddingTop="?android:attr/actionBarSize"

答案 1 :(得分:0)

您需要将app:layout_constraintTop_toTopOf="parent"更改为

app:layout_constraintTop_toBottomOf="@id/coordinator_layout"

当您提供约束作为父对象时,它将采用您的父布局容器,其中也包括工具栏。相反,您希望将其限制在工具栏下方。

答案 2 :(得分:0)

在首先设置ConstraintLayout的顶部添加T下方的最后一行:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"    
    app:layout_behavior="@string/appbar_scrolling_view_behavior"