无法使包含的视图与布局中的父级对齐

问题描述 投票:1回答:4

我试图在android中做一个简单的布局,包括另一个文件,它是一个包含三个按钮的底部导航。

我无法将包含的文件排在最底层。这是我的布局的xml(TextView显示与父底部对齐,但包含的文件没有):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/patientDOBBlock"
    tools:text="Test Navigation Text"
    android:layout_alignParentBottom="true"/>
<include
    android:id="@+id/navigation"
    android:layout_below="@+id/patientDOBBlock"
    layout="@layout/bottom_navigation"
    android:layout_height="66dp"
    android:layout_width="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

包含的文件:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/task_base_buttons"
android:layout_width="match_parent"
android:layout_height="66dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/btnTaskBack"
        style="@style/NeutrifProText.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="@string/action_back"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

    <Button
        android:id="@+id/btnTaskContinue"
        style="@style/NeutrifProText.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight="0.6"
        android:background="@drawable/rounded_rect_color_primary"
        android:text="@string/action_continue"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnTaskSkip"
        app:layout_constraintStart_toEndOf="@+id/btnTaskBack" />

    <Button
        android:id="@+id/btnTaskSkip"
        style="@style/NeutrifProText.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="@string/action_skip"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

 </android.support.constraint.ConstraintLayout>
 </merge>

知道为什么吗?顺便说一句,这个相同的包含文件显示在片段的底部。

编辑:我包括片段的布局,其中布局确实出现在底部

<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:id="@+id/task_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rangerhealth.rangerprovider.view.fragment.BaseTaskFragment"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:id="@+id/task_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="50dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="@dimen/title_font_size"
    tools:text="Title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/task_subtitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/task_title"
    android:layout_centerHorizontal="true"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="17dp"
    android:gravity="center"
    tools:text="Subtitle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/task_title" />

<TextView
    android:id="@+id/task_description"
    android:layout_width="match_parent"
    android:layout_height="21dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    tools:text="Description"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/task_subtitle" />


<include layout="@layout/bottom_navigation"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    />

<RelativeLayout
    android:id="@+id/task_placeholder"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/task_description"
    app:layout_constraintBottom_toTopOf="@id/task_base_buttons"
    >

</RelativeLayout>
</android.support.constraint.ConstraintLayout>
android android-layout android-relativelayout
4个回答
2
投票

在布局中,您将包含的布局与父底部和patientDOBBlock下方对齐。这两个指令存在冲突。我假设您希望底部导航和文本视图都与父底部对齐并重叠。

在包含的布局中删除以下行:

android:layout_below="@+id/patientDOBBlock"

这留下了以下布局:

<RelativeLayout 
    android:id="@+id/activity_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/patientDOBBlock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        tools:text="Test Navigation Text" />

    <include
        android:id="@+id/navigation"
        layout="@layout/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent" />
</RelativeLayout>

在bottom_navigation.xml中,删除merge标记。来自documentation

在一个布局中包含一个布局时,该标记有助于消除视图层次结构中的冗余视图组。

由于您需要将ConstraintLayout保留在包含的文件中,因此您没有冗余视图组,因此您不需要合并标记。使用merge标记还可以防止将布局参数应用于包含文件。见this

您还可以通过在标记中指定它们来覆盖所包含布局的根视图的所有布局参数(任何android:layout_ *属性)。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/task_base_buttons"
    android:layout_width="match_parent"
    android:layout_height="66dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/btnTaskBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="action_back"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

    <Button
        android:id="@+id/btnTaskContinue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight="0.6"
        android:background="@color/colorPrimary"
        android:text="action_continue"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnTaskSkip"
        app:layout_constraintStart_toEndOf="@+id/btnTaskBack" />

    <Button
        android:id="@+id/btnTaskSkip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="action_skip"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

</android.support.constraint.ConstraintLayout>

结果如下:

enter image description here

虽然它没有伤害任何东西,但ConstraintLayout中指定的约束将没有效果,除非父级也是ConstraintLayout,所以它们可以被删除。


1
投票

使用以下内容更改XML。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">


    <TextView
        android:id="@+id/patientDOBBlock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/navigation"
        android:gravity="center"
        tools:text="Test Navigation Text" />

    <include
        android:id="@+id/navigation"
        layout="@layout/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

bottom_navigation.xml

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/task_base_buttons"
android:layout_width="match_parent"
android:layout_height="66dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/btnTaskBack"
        style="@style/NeutrifProText.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="@string/action_back"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

    <Button
        android:id="@+id/btnTaskContinue"
        style="@style/NeutrifProText.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight="0.6"
        android:background="@drawable/rounded_rect_color_primary"
        android:text="@string/action_continue"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnTaskSkip"
        app:layout_constraintStart_toEndOf="@+id/btnTaskBack" />

    <Button
        android:id="@+id/btnTaskSkip"
        style="@style/NeutrifProText.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="@string/action_skip"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

 </android.support.constraint.ConstraintLayout>

我希望它对你有用。

enter image description here


1
投票

实际上,我必须添加一个LinearLayout,然后它全部工作:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">


<TextView
    android:id="@+id/patientDOBBlock"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/navigation"
    android:gravity="center"
    tools:text="Test Navigation Text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/navigation">

<include
    android:id="@+id/constraintLayoutNavigation"
    layout="@layout/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="66dp"
    android:layout_alignParentBottom="true" />
</LinearLayout>

</RelativeLayout>

0
投票
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/patientDOBBlock"
tools:text="Test Navigation Text"
android:layout_above = "@+id/navigation"/>

<include
android:id="@id/navigation"
android:layout_alignParentBottom = "true"
layout="@layout/bottom_navigation"
android:layout_height="66dp"
android:layout_width="match_parent"
/>
</RelativeLayout>

这应该完美

© www.soinside.com 2019 - 2024. All rights reserved.